The Open Standard for Generative UI
Docs Cleanup (#106)
Hrijul Bhatnagar197fbb279d68
diff --git a/docs/custom/reference/js/react-core/interfaces/ResponseTemplate.md b/docs/custom/reference/js/react-core/interfaces/ResponseTemplate.mdindex 70c0db275..a7d22ce75 100644--- a/docs/custom/reference/js/react-core/interfaces/ResponseTemplate.md+++ b/docs/custom/reference/js/react-core/interfaces/ResponseTemplate.md@@ -1,3 +1,3 @@A type that contains information about the name of the response template and the React component required to render the response template with that name.For detailed information on response templates, see [Core Concepts](../../../../concepts/001-core-concepts.md#response-templates), and for a guide on how to implement and use response templates,-see [Creating Response Templates](../../../../guides/customization/007-response-templates.mdx).+see [Creating Response Templates](../../../../guides/customization/005-response-templates.mdx).diff --git a/docs/docs/concepts/001-core-concepts.md b/docs/docs/concepts/001-core-concepts.mdindex c6dc22b87..a08547167 100644--- a/docs/docs/concepts/001-core-concepts.md+++ b/docs/docs/concepts/001-core-concepts.md@@ -24,7 +24,7 @@ The following image shows one of the available shells - The "Standalone" Shell.-For a detailed guide on how to customize the UI, see <ins>[Customizing Crayon UI](../guides/customization/009-customizing-ui.mdx).</ins>+For a detailed guide on how to customize the UI, see <ins>[Customizing Crayon UI](../guides/customization/004-customizing-ui.mdx).</ins>## Thread@@ -59,7 +59,7 @@ This system allows you to:- Define custom response formats for the agent response- Ensure type-safe and predicable props for your React components-For a detailed implementation guide, see <ins>[Creating Response Templates](../guides/customization/007-response-templates.mdx).</ins>+For a detailed implementation guide, see <ins>[Creating Response Templates](../guides/customization/005-response-templates.mdx).</ins>## Structured Outputs@@ -67,7 +67,7 @@ If you are using <ins>[Response Templates](#response-templates)</ins>, you wouldThis is accomplished by providing a schema to the LLM that instructs it on how to structure its response. This part is generally handled by the backend API that calls the LLM.A detailed example of how structured outputs can be implemented in a backend API can be found at --<ins>[Creating Response Templates: Backend API](../guides/customization/007-response-templates.mdx#step-3-creating-a-backend-endpoint-to-tell-the-llm-about-the-schema).</ins>+<ins>[Creating Response Templates: Backend API](../guides/customization/005-response-templates.mdx#step-3-creating-a-backend-endpoint-to-tell-the-llm-about-the-schema).</ins>Here's a sequence diagram that shows how response templates and structured outputs work together:diff --git a/docs/docs/concepts/002-generative-ui.md b/docs/docs/concepts/002-generative-ui.mdindex d568f3092..d8e5ab18a 100644--- a/docs/docs/concepts/002-generative-ui.md+++ b/docs/docs/concepts/002-generative-ui.md@@ -26,4 +26,4 @@ to generate structured outputs passing it a schema of the possibleoutputs - the name of the template and the arguments it takes.## How to use it?-See the [Generative UI guide](/docs/guides/generative-ui) for more information.+See the [Getting Started with Crayon](/docs/guides/getting-started) guide for more information.diff --git a/docs/docs/examples.md b/docs/docs/examples.mdnew file mode 100644index 000000000..63829d78a--- /dev/null+++ b/docs/docs/examples.md@@ -0,0 +1,28 @@+# Examples++:::info+You can find example projects demonstrating Crayon's capabilities in real-world applications.++These examples provide practical implementations that can help you understand how to integrate Crayon into your own projects.+:::++## Simple Cookbook++A TypeScript-based cookbook application that showcases how to integrate Crayon with Next.js. This example demonstrates:++- Recipe generation using AI+- Structured output handling for ingredients and instructions+- Real-time recipe generation+- Integration with OpenAI's API++<ins>[View Simple Cookbook Example →](https://github.com/thesysdev/examples/tree/main/simple-cookbook)</ins>++## Fino Crayon++A Next.js project that demonstrates more advanced usage of Crayon by building a personal finance assistant. This example demonstrates:++- Cutomizing the Crayon UI by using individual components and `ChatProvider` instead of the pre-built `CrayonChat` component.+- Database integration with Prisma+- Full-stack implementation++<ins>[View Fino Crayon Example →](https://github.com/thesysdev/examples/tree/main/fino-crayon)</ins>diff --git a/docs/docs/guides/001-generative-ui.mdx b/docs/docs/guides/001-generative-ui.mdxdeleted file mode 100644index 495e979de..000000000--- a/docs/docs/guides/001-generative-ui.mdx+++ /dev/null@@ -1,325 +0,0 @@-# Generative UI--import Tabs from '@theme/Tabs';-import TabItem from '@theme/TabItem';--This guide demonstrates how to build a generative UI application using structured outputs and streaming. We'll build a simple recipe generator that shows how to:-1. Define structured templates for LLM responses-2. Stream responses to the UI-3. Render dynamic components based on LLM output--## Project Structure-```-src/-├── app/-│ ├── page.tsx # Main chat interface-│ ├── api/-│ │ └── chat/-│ │ └── route.ts # API endpoint-│ └── templates/-│ └── recipe.tsx # Recipe template component-└── types/- └── recipe.ts # Type definitions and schema-```--## Understanding Structured Outputs--When building generative UI applications, we want the LLM to generate responses that our UI can understand and render consistently. Instead of dealing with raw text or markdown, we can define structured templates that the LLM must follow.--### Example: Recipe Generator--Let's build a recipe generator that produces structured recipe data. First, we'll define our schema:--<Tabs groupId="backend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="types/recipe.ts"-import { z } from "zod";--export const RecipeTemplateSchema = z.object({- title: z.string(),- cuisine: z.string(),- cookingTime: z.number(),- ingredients: z.array(z.string()),- instructions: z.array(z.string())-});--export type RecipeTemplateProps = z.infer<typeof RecipeTemplateSchema>;-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="types/recipe.js"-import { z } from "zod";--export const RecipeTemplateSchema = z.object({- title: z.string(),- cuisine: z.string(),- cookingTime: z.number(),- ingredients: z.array(z.string()),- instructions: z.array(z.string())-});-```- </TabItem>-</Tabs>--## Building the UI Components--We create React components that can render our structured data. Here's a simplified recipe component:--<Tabs groupId="frontend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="app/templates/recipe.tsx"-import { Card } from "@crayonai/react-ui";-import { RecipeTemplateProps } from "@/types/recipe";--export const RecipeTemplate: React.FC<RecipeTemplateProps> = ({- title,- cuisine,- cookingTime,- ingredients,- instructions,-}) => {- return (- <Card>- <h2>{title}</h2>- <p>{cuisine} • {cookingTime} mins</p>-- <h3>Ingredients</h3>- <ul>- {ingredients.map((ingredient, i) => (- <li key={i}>{ingredient}</li>- ))}- </ul>-- <h3>Instructions</h3>- <ol>- {instructions.map((step, i) => (- <li key={i}>{step}</li>- ))}- </ol>- </Card>- );-};-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="app/templates/recipe.jsx"-import { Card } from "@crayonai/react-ui";--export const RecipeTemplate = ({- title,- cuisine,- cookingTime,- ingredients,- instructions,-}) => {- return (- <Card>- <h2>{title}</h2>- <p>{cuisine} • {cookingTime} mins</p>-- <h3>Ingredients</h3>- <ul>- {ingredients.map((ingredient, i) => (- <li key={i}>{ingredient}</li>- ))}- </ul>-- <h3>Instructions</h3>- <ol>- {instructions.map((step, i) => (- <li key={i}>{step}</li>- ))}- </ol>- </Card>- );-};-```- </TabItem>-</Tabs>--## Setting Up the Chat Interface--The main chat interface uses `CrayonChat` which handles the streaming protocol and state management:--<Tabs groupId="frontend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="app/page.tsx"-"use client";--import type { Message } from "@crayonai/react-core";-import { CrayonChat } from "@crayonai/react-ui";-import "@crayonai/react-ui/styles/index.css";-import { RecipeTemplate } from "./templates/recipe";--const processMessage = async ({- threadId,- messages,- abortController,-}: {- threadId: string;- messages: Message[];- abortController: AbortController;-}) => {- const response = await fetch("/api/chat", {- method: "POST",- body: JSON.stringify({ threadId, messages }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });- return response;-};--export default function Home() {- return (- <CrayonChat- processMessage={processMessage}- responseTemplates={[- {- name: "recipe",- Component: RecipeTemplate,- }- ]}- />- );-}-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="app/page.jsx"-"use client";--import { CrayonChat } from "@crayonai/react-ui";-import "@crayonai/react-ui/styles/index.css";-import { RecipeTemplate } from "./templates/recipe";--const processMessage = async ({ threadId, messages, abortController }) => {- const response = await fetch("/api/chat", {- method: "POST",- body: JSON.stringify({ threadId, messages }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });- return response;-};--export default function Home() {- return (- <CrayonChat- processMessage={processMessage}- responseTemplates={[- {- name: "recipe",- Component: RecipeTemplate,- }- ]}- />- );-}-```- </TabItem>-</Tabs>--## Handling API Responses--The API route handles communication with the LLM and streams the responses:--<Tabs groupId="backend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="app/api/chat/route.ts"-import { NextRequest } from "next/server";-import OpenAI from "openai";-import {- fromOpenAICompletion,- toOpenAIMessages,- templatesToResponseFormat,-} from "@crayonai/stream";-import { RecipeTemplateSchema } from "@/types/recipe";--export async function POST(req: NextRequest) {- const { messages } = await req.json();- const client = new OpenAI();-- // Convert our template schema into OpenAI's response format- const responseFormat = templatesToResponseFormat({- schema: RecipeTemplateSchema,- name: "recipe",- description: "Use this template to generate a recipe"- });-- // Stream the LLM response- const llmStream = await client.chat.completions.create({- model: "gpt-4",- messages: toOpenAIMessages(messages),- stream: true,- response_format: responseFormat,- });-- // Convert OpenAI stream to our streaming protocol- const responseStream = fromOpenAICompletion(llmStream);-- return new Response(responseStream, {- headers: {- "Content-Type": "text/event-stream",- "Cache-Control": "no-cache, no-transform",- Connection: "keep-alive",- },- });-}-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="app/api/chat/route.js"-import OpenAI from "openai";-import {- fromOpenAICompletion,- toOpenAIMessages,- templatesToResponseFormat,-} from "@crayonai/stream";-import { RecipeTemplateSchema } from "@/types/recipe";--export async function POST(req) {- const { messages } = await req.json();- const client = new OpenAI();-- // Convert our template schema into OpenAI's response format- const responseFormat = templatesToResponseFormat({- schema: RecipeTemplateSchema,- name: "recipe",- description: "Use this template to generate a recipe"- });-- // Stream the LLM response- const llmStream = await client.chat.completions.create({- model: "gpt-4",- messages: toOpenAIMessages(messages),- stream: true,- response_format: responseFormat,- });-- // Convert OpenAI stream to our streaming protocol- const responseStream = fromOpenAICompletion(llmStream);-- return new Response(responseStream, {- headers: {- "Content-Type": "text/event-stream",- "Cache-Control": "no-cache, no-transform",- Connection: "keep-alive",- },- });-}-```- </TabItem>-</Tabs>--## Key Benefits--1. **Type Safety**: The schema ensures the LLM generates responses in a predictable format-2. **Real-time Updates**: Streaming enables immediate feedback to users-3. **Component-based**: Separate templates for different response types-4. **Structured Data**: All responses follow a predefined schema for consistencydiff --git a/docs/docs/guides/001-getting-started.mdx b/docs/docs/guides/001-getting-started.mdxnew file mode 100644index 000000000..06d8fb538--- /dev/null+++ b/docs/docs/guides/001-getting-started.mdx@@ -0,0 +1,225 @@+import Tabs from "@theme/Tabs";+import TabItem from "@theme/TabItem";++# Getting Started with Crayon++Crayon 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`++The `processMessage` function is the core of your chat implementation. It receives three parameters:++- `threadId`: A unique identifier for the chat conversation+- `messages`: An array containing all messages in the conversation, including the new one+- `abortController`: Used to cancel the request if needed++This function should make an API call to your backend and return a response following the <ins>[Streaming Protocol](../concepts/003-data-format.mdx)</ins>. Here's a basic example that calls a `/api/chat` endpoint:++<Tabs groupId="frontend-lang">+<TabItem value="ts" label="TypeScript">+```tsx+const processMessage = async ({+ threadId,+ messages,+ abortController,+}: {+ threadId: string;+ messages: Message[];+ abortController: AbortController;+}) => {+ const response = await fetch("/api/chat", {+ method: "POST",+ body: JSON.stringify({ threadId, messages }),+ headers: {+ "Content-Type": "application/json",+ Accept: "text/event-stream",+ },+ signal: abortController.signal,+ });+ return response;+};+```+</TabItem>+<TabItem value="js" label="JavaScript">+```jsx+const processMessage = async ({+ threadId,+ messages,+ abortController,+}) => {+ const response = await fetch("/api/chat", {+ method: "POST",+ body: JSON.stringify({ threadId, messages }),+ headers: {+ "Content-Type": "application/json",+ Accept: "text/event-stream",+ },+ signal: abortController.signal,+ });+ return response;+};+```+</TabItem>+</Tabs>++## Step 2: 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>.++Here's a simple example that renders text in red:++<Tabs groupId="frontend-lang">+<TabItem value="ts" label="TypeScript">+```tsx+export const TextTemplate: React.FC<{ children: React.ReactNode }> = ({+ children,+}) => {+ return <div style={{color: 'red'}}>{children}</div>;+};+```+</TabItem>+<TabItem value="js" label="JavaScript">+```jsx+export const TextTemplate = ({ children }) => {+ return <div style={{color: 'red'}}>{children}</div>;+};+```+</TabItem>+</Tabs>++## Step 3: 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:++<Tabs groupId='frontend-lang'>++<TabItem value="ts" label="TypeScript">++```ts+import { NextRequest } from "next/server";+import OpenAI from "openai";+import { fromOpenAICompletion, toOpenAIMessages } from "@crayonai/stream";+import { Message } from "@crayonai/react-core";++const textResponseSchema = {+ type: "object",+ description: "Use this schema to generate a text response",+ properties: {+ type: { const: "text" },+ text: {+ type: "string",+ description: "Plaintext message to be displayed to the user",+ },+ },+ required: ["type", "text"],+ additionalProperties: false,+};++export async function POST(req: NextRequest) {+ const { messages } = (await req.json()) as { messages: Message[] };+ const client = new OpenAI();+ const llmStream = await client.chat.completions.create({+ model: "gpt-4o",+ messages: toOpenAIMessages(+ messages+ ) as OpenAI.Chat.ChatCompletionMessageParam[],+ stream: true,+ response_format: textResponseSchema,+ });+ const responseStream = fromOpenAICompletion(llmStream);+ return new Response(responseStream as unknown as ReadableStream<Uint8Array>, {+ headers: {+ "Content-Type": "text/event-stream",+ "Cache-Control": "no-cache, no-transform",+ Connection: "keep-alive",+ },+ });+}+```++</TabItem>++<TabItem value="js" label="JavaScript">++```js+import OpenAI from "openai";+import { fromOpenAICompletion, toOpenAIMessages } from "@crayonai/stream";++const textResponseSchema = {+ type: "object",+ description: "Use this schema to generate a text response",+ properties: {+ type: { const: "text" },+ text: {+ type: "string",+ description: "Plaintext message to be displayed to the user",+ },+ },+ required: ["type", "text"],+ additionalProperties: false,+};++export async function POST(req) {+ const { messages } = await req.json();+ const client = new OpenAI();+ const llmStream = await client.chat.completions.create({+ model: "gpt-4o",+ messages: toOpenAIMessages(messages),+ stream: true,+ response_format: textResponseSchema,+ });+ const responseStream = fromOpenAICompletion(llmStream);+ return new Response(responseStream, {+ headers: {+ "Content-Type": "text/event-stream",+ "Cache-Control": "no-cache, no-transform",+ Connection: "keep-alive",+ },+ });+}+```++</TabItem>++</Tabs>++## Step 4: Putting it all together++Now we can combine everything into a working chat interface. The `CrayonChat` component will handle:++- Rendering the chat UI+- Managing message threads+- Making API calls+- Displaying responses using your templates++Here's how to set it up:++<Tabs groupId="frontend-lang">+ <TabItem value="ts" label="TypeScript">+ ```tsx+ <CrayonChat+ processMessage={processMessage}+ responseTemplates={[+ {+ name: "text",+ Component: TextTemplate,+ },+ ]}+ />+ ```+ </TabItem>+ <TabItem value="js" label="JavaScript">+ ```jsx+ <CrayonChat+ processMessage={processMessage}+ responseTemplates={[+ {+ name: "text",+ Component: TextTemplate,+ },+ ]}+ />+ ```+ </TabItem>+</Tabs>diff --git a/docs/docs/guides/002-default-text.mdx b/docs/docs/guides/002-default-text.mdxdeleted file mode 100644index bd40c699a..000000000--- a/docs/docs/guides/002-default-text.mdx+++ /dev/null@@ -1,109 +0,0 @@-import Tabs from "@theme/Tabs";-import TabItem from "@theme/TabItem";--# Default Renderer--The default text renderer is a fundamental component that enables your Crayon Chat application to display formatted text responses from AI models. By implementing this renderer, you can:--- Display AI responses with proper formatting using Markdown-- Support rich text features like **bold**, *italic*, lists, and code blocks-- Maintain consistent text styling across your chat interface-- Handle basic text responses without needing complex custom templates--This guide shows how to add a text renderer template to your Crayon Chat application. This assumes you have already set up your Next.js project following the [NextJS Quickstart Guide](/quickstart/002-nextjs.mdx).--## Install Required Dependencies--First, install the markdown renderer:--<Tabs groupId="js-package-manager">- <TabItem value="npm" label="npm">-```bash-npm install react-markdown-```- </TabItem>- <TabItem value="pnpm" label="pnpm">-```bash-pnpm add react-markdown-```- </TabItem>- <TabItem value="yarn" label="yarn">-```bash-yarn add react-markdown-```- </TabItem>-</Tabs>--## Create Text Template--Create a new file for your text template:--<Tabs groupId="frontend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="app/templates/text.tsx"-import React from "react";-import Markdown from "react-markdown";--export const TextTemplate: React.FC<{ children: React.ReactNode }> = ({- children,-}) => {- return <Markdown>{children as string}</Markdown>;-};-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="app/templates/text.jsx"-import React from "react";-import Markdown from "react-markdown";--export const TextTemplate = ({ children }) => {- return <Markdown>{children}</Markdown>;-};-```- </TabItem>-</Tabs>--## Update Your Page Component--If you've followed the Quick Start guide, just add the text template to your existing templates array:--<Tabs groupId="frontend-lang">- <TabItem value="ts" label="TypeScript">-```typescript title="app/page.tsx"-import { TextTemplate } from "@/app/templates/text";--const templates = [- {- name: "text",- Component: TextTemplate- }-];--// Use in your CrayonChat component-<CrayonChat- processMessage={processMessage} // Required for NextJS setup- responseTemplates={templates}-/>-```- </TabItem>- <TabItem value="js" label="JavaScript">-```javascript title="app/page.jsx"-import { TextTemplate } from "@/app/templates/text";--const templates = [- {- name: "text",- Component: TextTemplate- }-];--// Use in your CrayonChat component-<CrayonChat- processMessage={processMessage} // Required for NextJS setup- responseTemplates={templates}-/>-```- </TabItem>-</Tabs>--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/002-rendering-markdown.mdx b/docs/docs/guides/002-rendering-markdown.mdxnew file mode 100644index 000000000..09ffc97f8--- /dev/null+++ b/docs/docs/guides/002-rendering-markdown.mdx@@ -0,0 +1,211 @@+---+description: Rendering markdown formatted agent responses in Crayon+---++import Tabs from "@theme/Tabs";+import TabItem from "@theme/TabItem";++# Rendering Markdown++A markdown renderer is a fundamental component that enables your Crayon Chat application to display formatted text responses from AI models. By implementing this renderer, you can:++- Display AI responses with proper formatting using Markdown+- Support rich text features like **bold**, _italic_, lists, and code blocks+- Maintain consistent text styling across your chat interface+- Handle basic text responses without needing complex custom templates++This 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 following+the [NextJS Quickstart Guide](/quickstart/002-nextjs.mdx).++## Step 1: Install Required Dependencies++First, install the markdown renderer:++<Tabs groupId="js-package-manager">+ <TabItem value="npm" label="npm">+ ```bash npm install react-markdown ```+ </TabItem>+ <TabItem value="pnpm" label="pnpm">+ ```bash pnpm add react-markdown ```+ </TabItem>+ <TabItem value="yarn" label="yarn">+ ```bash yarn add react-markdown ```+ </TabItem>+</Tabs>++## Step 2: Create Markdown Template++Create a new file for your markdown template:++<Tabs groupId="frontend-lang">+ <TabItem value="ts" label="TypeScript">+```typescript title="app/templates/markdown.tsx"+import React from "react";+import Markdown from "react-markdown";++export const MarkdownTemplate: React.FC<{ children: React.ReactNode }> = ({+ children,+}) => {+ return <Markdown>{children as string}</Markdown>;+};+```+ </TabItem>+ <TabItem value="js" label="JavaScript">+```javascript title="app/templates/markdown.jsx"+import React from "react";+import Markdown from "react-markdown";++export const MarkdownTemplate = ({ children }) => {+ return <Markdown>{children}</Markdown>;+};+```+ </TabItem>+</Tabs>++## Step 3: Update Your Page Component++If you've followed the Quick Start guide, just add the text template to your existing templates array:++<Tabs groupId="frontend-lang">+ <TabItem value="ts" label="TypeScript">++```typescript title="app/page.tsx"+import { MarkdownTemplate } from "@/app/templates/markdown";++const YourComponent = () => {+ // ... implement processMessage as shown in the 'Getting Started with Crayon' guide++ return (+ <CrayonChat+ processMessage={processMessage} // Required for NextJS setup+ responseTemplates={[{+ name: "text",+ Component: MarkdownTemplate+ }]}+ />+ )+}+```++ </TabItem>+ <TabItem value="js" label="JavaScript">++```javascript title="app/page.jsx"+import { TextTemplate } from "@/app/templates/text";++const templates = [+ {+ name: "text",+ Component: TextTemplate,+ },+];++// Use in your CrayonChat component++<CrayonChat+ processMessage={processMessage} // Required for NextJS setup+ responseTemplates={templates}+/>;+```++ </TabItem>+</Tabs>++## Step 4: 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 look+something like this:++<Tabs groupId='frontend-lang'>++<TabItem value="ts" label="TypeScript">++```ts+import { NextRequest } from "next/server";+import OpenAI from "openai";+import { fromOpenAICompletion, toOpenAIMessages } from "@crayonai/stream";+import { Message } from "@crayonai/react-core";++const textResponseSchema = {+ type: "object",+ description: "Use this schema to generate a markdown formatted text response",+ properties: {+ type: { const: "text" },+ text: {+ type: "string",+ description: "Markdown formatted message to be displayed to the user",+ },+ },+ required: ["type", "text"],+ additionalProperties: false,+};++export async function POST(req: NextRequest) {+ const { messages } = (await req.json()) as { messages: Message[] };+ const client = new OpenAI();+ const llmStream = await client.chat.completions.create({+ model: "gpt-4o",+ messages: toOpenAIMessages(+ messages+ ) as OpenAI.Chat.ChatCompletionMessageParam[],+ stream: true,+ response_format: textResponseSchema,+ });+ const responseStream = fromOpenAICompletion(llmStream);+ return new Response(responseStream as unknown as ReadableStream<Uint8Array>, {+ headers: {+ "Content-Type": "text/event-stream",+ "Cache-Control": "no-cache, no-transform",+ Connection: "keep-alive",+ },+ });+}+```++</TabItem>++<TabItem value="js" label="JavaScript">++```js+import OpenAI from "openai";+import { fromOpenAICompletion, toOpenAIMessages } from "@crayonai/stream";++const textResponseSchema = {+ type: "object",+ description: "Use this schema to generate a markdown response",+ properties: {+ type: { const: "text" },+ text: {+ type: "string",+ description: "Markdown formatted message to be displayed to the user",+ },+ },+ required: ["type", "text"],+ additionalProperties: false,+};++export async function POST(req) {+ const { messages } = await req.json();+ const client = new OpenAI();+ const llmStream = await client.chat.completions.create({+ model: "gpt-4o",+ messages: toOpenAIMessages(messages),+ stream: true,+ response_format: textResponseSchema,+ });+ const responseStream = fromOpenAICompletion(llmStream);+ return new Response(responseStream, {+ headers: {+ "Content-Type": "text/event-stream",+ "Cache-Control": "no-cache, no-transform",+ Connection: "keep-alive",+ },+ });+}+```++</TabItem>++</Tabs>++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/004-connecting-thread-backend.md b/docs/docs/guides/004-connecting-thread-backend.mddeleted file mode 100644index e476d629b..000000000--- a/docs/docs/guides/004-connecting-thread-backend.md+++ /dev/null@@ -1,153 +0,0 @@-# Connecting to a Simple Thread Backend--This guide demonstrates how to implement a simple in-memory thread backend for Crayon Chat.--## Backend Implementation--First, create a simple in-memory store for your threads and messages:--```typescript title="api/store.ts"-interface Message {- id: string;- role: 'user' | 'assistant';- message: string;- createdAt: Date;-}--interface Thread {- threadId: number;- title: string;- createdAt: Date;- status: 'active' | 'deleted';-}--// In-memory storage-const threads = new Map<number, Thread>();-const messages = new Map<number, Message[]>();-let nextThreadId = 1;-```--Create simple API routes for handling threads and messages:--```typescript title="api/threads/route.ts"-export async function GET() {- return Response.json({- threads: Array.from(threads.values())- .filter(t => t.status === 'active')- .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())- });-}--export async function POST(request: Request) {- const body = await request.json();- const thread: Thread = {- threadId: nextThreadId++,- title: body.title || 'New Chat',- createdAt: new Date(),- status: 'active'- };- threads.set(thread.threadId, thread);- messages.set(thread.threadId, []);- return Response.json(thread);-}-```--```typescript title="api/messages/route.ts"-export async function GET(request: Request) {- const { searchParams } = new URL(request.url);- const threadId = parseInt(searchParams.get('threadId') || '');- return Response.json({- messages: messages.get(threadId) || []- });-}--export async function POST(request: Request) {- const { threadId, message } = await request.json();- const threadMessages = messages.get(threadId) || [];- const newMessage = {- id: crypto.randomUUID(),- role: 'user',- message,- createdAt: new Date()- };- threadMessages.push(newMessage);- messages.set(threadId, threadMessages);- return Response.json(newMessage);-}-```--## Frontend Integration--Connect your frontend using the `CrayonChat` component with custom thread managers:--```tsx title="app/page.tsx"-import { CrayonChat } from "@crayonai/react-ui";-import { useThreadListManager, useThreadManager } from "@crayonai/react-core";--export default function Chat() {- const threadListManager = useThreadListManager({- fetchThreadList: async () => {- const response = await fetch('/api/threads');- const data = await response.json();- return data.threads;- },- createThread: async (firstMessage) => {- const response = await fetch('/api/threads', {- method: 'POST',- body: JSON.stringify({ title: firstMessage.message })- });- return response.json();- },- // Exercise for the reader- deleteThread: async () => {},- updateThread: async (t) => t,- onSwitchToNew: () => {},- onSelectThread: () => {}- });-- const threadManager = useThreadManager({- threadId: threadListManager.selectedThreadId,- shouldResetThreadState: threadListManager.shouldResetThreadState,- loadThread: async () => {- const response = await fetch(`/api/messages?threadId=${threadListManager.selectedThreadId}`);- const data = await response.json();- return data.messages;- },- onProcessMessage: async ({ message, threadManager }) => {- // Send message to your backend- const response = await fetch('/api/messages', {- method: 'POST',- body: JSON.stringify({- threadId: threadListManager.selectedThreadId,- message: message.message- })- });- const newMessage = await response.json();- threadManager.appendMessages(newMessage);- return [];- }- });-- return (- <CrayonChat- threadListManager={threadListManager}- threadManager={threadManager}- />- );-}-```--The `CrayonChat` component with custom thread managers gives you:--1. Full control over thread and message management-2. Custom thread listing and creation logic-3. Custom message processing and storage-4. Built-in UI components and state synchronization--This implementation provides a basic in-memory thread backend that supports:-- Creating new threads-- Listing existing threads-- Sending and receiving messages-- Persisting messages within a thread--Note that this in-memory implementation is suitable for development and testing. For production use, you should implement proper database storage and error handling.diff --git a/docs/docs/guides/006-getting-started.mdx b/docs/docs/guides/006-getting-started.mdxdeleted file mode 100644index 189955d7d..000000000--- a/docs/docs/guides/006-getting-started.mdx+++ /dev/null@@ -1,126 +0,0 @@-import Tabs from '@theme/Tabs';-import TabItem from '@theme/TabItem';--# Getting Started with Crayon--Crayon 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`--The `processMessage` function is the core of your chat implementation. It receives three parameters:-- `threadId`: A unique identifier for the chat conversation-- `messages`: An array containing all messages in the conversation, including the new one-- `abortController`: Used to cancel the request if needed--This function should make an API call to your backend and return a response following the <ins>[Streaming Protocol](../concepts/003-data-format.mdx)</ins>. Here's a basic example that calls a `/api/chat` endpoint:--<Tabs groupId="frontend-lang">-<TabItem value="ts" label="TypeScript">-```tsx-const processMessage = async ({- threadId,- messages,- abortController,-}: {- threadId: string;- messages: Message[];- abortController: AbortController;-}) => {- const response = await fetch("/api/chat", {- method: "POST",- body: JSON.stringify({ threadId, messages }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });- return response;-};-```-</TabItem>-<TabItem value="js" label="JavaScript">-```jsx-const processMessage = async ({- threadId,- messages,- abortController,-}) => {- const response = await fetch("/api/chat", {- method: "POST",- body: JSON.stringify({ threadId, messages }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });- return response;-};-```-</TabItem>-</Tabs>--## Step 2: 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/007-response-templates.mdx)</ins>.--Here's a simple example that renders text in red:--<Tabs groupId="frontend-lang">-<TabItem value="ts" label="TypeScript">-```tsx-export const TextTemplate: React.FC<{ children: React.ReactNode }> = ({- children,-}) => {- return <div style={{color: 'red'}}>{children}</div>;-};-```-</TabItem>-<TabItem value="js" label="JavaScript">-```jsx-export const TextTemplate = ({ children }) => {- return <div style={{color: 'red'}}>{children}</div>;-};-```-</TabItem>-</Tabs>--## Step 3: Putting it all together--Now we can combine everything into a working chat interface. The `CrayonChat` component will handle:-- Rendering the chat UI-- Managing message threads-- Making API calls-- Displaying responses using your templates--Here's how to set it up:--<Tabs groupId="frontend-lang">-<TabItem value="ts" label="TypeScript">-```tsx-<CrayonChat- processMessage={processMessage}- responseTemplates={[- {- name: "text",- Component: TextTemplate,- },- ]}-/>-```-</TabItem>-<TabItem value="js" label="JavaScript">-```jsx-<CrayonChat- processMessage={processMessage}- responseTemplates={[- {- name: "text",- Component: TextTemplate,- },- ]}-/>-```-</TabItem>-</Tabs>diff --git a/docs/docs/guides/customization/003-connecting-to-backend.mdx b/docs/docs/guides/customization/003-connecting-to-backend.mdxnew file mode 100644index 000000000..7edad2b90--- /dev/null+++ b/docs/docs/guides/customization/003-connecting-to-backend.mdx@@ -0,0 +1,244 @@+---+description: Connecting your backend with Crayon to fetch and persist data+---++import Tabs from "@theme/Tabs";+import TabItem from "@theme/TabItem";++# Connecting to Backend++Crayon is highly customizable in terms of UI, data and logic. If you are building an agent, chances are you would like to persist the threads that a user creates across sessions. Crayon exposes hooks that enable you to connect your backend APIs to list threads and their content.++Detailed explanations of all of the hooks mentioned in this guide is available in the <ins>[API Reference](../../reference/js/react-core).</ins>++## Managing the list of threads++The `useThreadListManager` hook takes multiple functions that let you control every action on the theads list. Here is one example of how it can be used to control the actions and sync with your backend++<Tabs groupId="frontend-lang">++<TabItem value="ts" label="TypeScript">+```tsx+ const threadListManager = useThreadListManager({+ fetchThreadList: async () => {+ const response = await fetch("/api/threads");+ const data = await response.json();+ return data.threads.map((thread: Thread) => ({+ threadId: +thread.threadId,+ title: thread.title,+ createdAt: new Date(thread.createdAt),+ isRunning: false,+ }));+ },+ createThread: (firstMessage) => {+ return fetch("/api/threads", {+ method: "POST",+ body: JSON.stringify({+ title: firstMessage.message!,+ }),+ }).then((res) => res.json());+ },+ deleteThread: async () => {}, // Called when the user deletes a thread. Use your delete thread API here+ updateThread: async (t) => t, // Called when the user makes changes to an existing thread. Use your update thread API here+ onSwitchToNew: () => {}, // Called when the user switches to a new thread.+ onSelectThread: () => {}, // Called when the user selects an existing thread.+ });+```++</TabItem>++<TabItem value="js" label="JavaScript">+```jsx+ const threadListManager = useThreadListManager({+ fetchThreadList: async () => {+ const response = await fetch("/api/threads");+ const data = await response.json();+ return data.threads.map((thread) => ({+ threadId: Number(thread.threadId),+ title: thread.title,+ createdAt: new Date(thread.createdAt),+ isRunning: false,+ }));+ },+ createThread: (firstMessage) => {+ return fetch("/api/threads", {+ method: "POST",+ body: JSON.stringify({+ title: firstMessage.message,+ }),+ }).then((res) => res.json());+ },+ deleteThread: async () => {}, // Called when the user deletes a thread. Use your delete API here+ updateThread: async (t) => t, // Called when the user makes changes to an existing thread. Use your update thread API here+ onSwitchToNew: () => {}, // Called when the user switches threads. This can be used to clear the chat when switching between threads+ onSelectThread: () => {}, // Called when the user selects an existing thread.+ });+```++</TabItem>++</Tabs>++## Managing data inside a thread++The `useThreadManager` hook lets you handle the data and events inside a thread. Here's an example of how you can manage the selected thread. The `threadListManager` returned by `useThreadListManager` makes things easier by providing a few things you might need.++<Tabs groupId="frontend-lang">++<TabItem value="ts" label="TypeScript">++```tsx+const threadManager = useThreadManager({+ threadId: threadListManager.selectedThreadId,+ shouldResetThreadState: threadListManager.shouldResetThreadState,+ loadThread: async () => {+ // Called to load the data for this specific thread. In this case, you would use your backend API that returns the selected thread's messages+ const response = await fetch(+ `/api/messages?threadId=${threadListManager.selectedThreadId}`+ );+ const data = await response.json();+ return data.messages;+ },+ onProcessMessage: async ({ message, threadManager, abortController }) => {+ // This is called when the user posts a message. Here, you might want to call your backend API that pushes messages inside the selected thread.+ threadManager.appendMessages({+ id: crypto.randomUUID(),+ role: "user",+ type: "prompt",+ message: message.message as string,+ });++ let threadId = threadListManager.selectedThreadId;+ // If this is a new thread that currently has not been created on the backend, create a thread, otherwise push message to an existing thread+ if (!threadId) {+ const thread = await threadListManager.createThread(+ message as UserMessage+ );+ threadListManager.selectThread(thread.threadId!, false);+ threadId = thread.threadId;+ }+ const response = await fetch("/api/ask", {+ method: "POST",+ body: JSON.stringify({ threadId, ...message }),+ headers: {+ "Content-Type": "application/json",+ Accept: "text/event-stream",+ },+ signal: abortController.signal,+ });++ // This is a utility function provided by crayon to make handling streamed messages more convenient.+ await processStreamedMessage({+ response,+ createMessage: threadManager.appendMessages,+ updateMessage: threadManager.updateMessage,+ deleteMessage: (messageId) => {+ const newMessages = threadManager.messages.filter(+ (message) => message.id !== messageId+ );+ threadManager.setMessages(newMessages);+ },+ });++ // ... call your backend API to persist the messages here++ return [];+ },+ responseTemplates: templates,+});+```++</TabItem>++<TabItem value="js" label="JavaScript">++```jsx+const threadManager = useThreadManager({+ threadId: threadListManager.selectedThreadId,+ shouldResetThreadState: threadListManager.shouldResetThreadState,+ loadThread: async () => {+ // Called to load the data for this specific thread. In this case, you would use your backend API that returns the selected thread's messages+ const response = await fetch(+ `/api/messages?threadId=${threadListManager.selectedThreadId}`+ );+ const data = await response.json();+ return data.messages;+ },+ onProcessMessage: async ({ message, threadManager, abortController }) => {+ // This is called when the user posts a message. Here, you might want to call your backend API that pushes messages inside the selected thread.+ threadManager.appendMessages({+ id: crypto.randomUUID(),+ role: "user",+ type: "prompt",+ message: message.message,+ });++ let threadId = threadListManager.selectedThreadId;+ // If this is a new thread that currently has not been created on the backend, create a thread, otherwise push message to an existing thread+ if (!threadId) {+ const thread = await threadListManager.createThread(message);+ threadListManager.selectThread(thread.threadId, false);+ threadId = thread.threadId;+ }+ const response = await fetch("/api/ask", {+ method: "POST",+ body: JSON.stringify({ threadId, ...message }),+ headers: {+ "Content-Type": "application/json",+ Accept: "text/event-stream",+ },+ signal: abortController.signal,+ });++ // This is a utility function provided by crayon to make handling streamed messages more convenient.+ await processStreamedMessage({+ response,+ createMessage: threadManager.appendMessages,+ updateMessage: threadManager.updateMessage,+ deleteMessage: (messageId) => {+ const newMessages = threadManager.messages.filter(+ (message) => message.id !== messageId+ );+ threadManager.setMessages(newMessages);+ },+ });+ return [];+ },+ responseTemplates: templates,+});+```++</TabItem>++</Tabs>++---++:::tip+The `threadListManager` and `threadManager` can then be passed to multiple crayon components such as the `ChatProvider` and `CrayonChat` to customize them to your needs. This enables usage of the Crayon UI with any type of backend.+:::++---++## Passing `ThreadManager` and `ThreadListManager` to `CrayonChat`++The simplest way to use [`ThreadManager`](../../reference/js/react-core/type-aliases/ThreadManager.md) and [`ThreadListManager`](../../reference/js/react-core/type-aliases/ThreadListManager.md) is+to pass them to the `CrayonChat` component as props:++```tsx+const YourComponent = () => {+ const threadManager = useThreadManager({+ // ... implement as shown above+ });+ const threadListManager = useThreadListManager({+ // ... implement as shown above+ });++ return (+ <CrayonChat+ threadManager={threadManager}+ threadListManager={threadListManager}+ />+ );+};+```diff --git a/docs/docs/guides/customization/009-customizing-ui.mdx b/docs/docs/guides/customization/004-customizing-ui.mdxsimilarity index 100%rename from docs/docs/guides/customization/009-customizing-ui.mdxrename to docs/docs/guides/customization/004-customizing-ui.mdxdiff --git a/docs/docs/guides/customization/007-response-templates.mdx b/docs/docs/guides/customization/005-response-templates.mdxsimilarity index 99%rename from docs/docs/guides/customization/007-response-templates.mdxrename to docs/docs/guides/customization/005-response-templates.mdxindex 155670c3c..d82dcf40d 100644--- a/docs/docs/guides/customization/007-response-templates.mdx+++ b/docs/docs/guides/customization/005-response-templates.mdx@@ -194,7 +194,7 @@ export async function POST(req) {</TabItem></Tabs>-This API can then be used in the `processMessage` method as shown in <ins>[Getting started with Crayon](../006-getting-started.mdx)</ins>, and this response template can be passed to `CrayonChat` as follows:+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/guides/customization/008-connecting-to-backend.mdx b/docs/docs/guides/customization/008-connecting-to-backend.mdxdeleted file mode 100644index 588036118..000000000--- a/docs/docs/guides/customization/008-connecting-to-backend.mdx+++ /dev/null@@ -1,202 +0,0 @@-----description: Connecting your backend with Crayon to fetch and persist data------import Tabs from '@theme/Tabs';-import TabItem from '@theme/TabItem';--# Connecting to Backend--Crayon is highly customizable in terms of UI, data and logic. If you are building an agent, chances are you would like to persist the threads that a user creates across sessions. Crayon exposes hooks that enable you to connect your backend APIs to list threads and their content.--Detailed explanations of all of the hooks mentioned in this guide is available in the <ins>[API Reference](../../reference/js/react-core).</ins>--## Managing the list of threads--The `useThreadListManager` hook takes multiple functions that let you control every action on the theads list. Here is one example of how it can be used to control the actions and sync with your backend--<Tabs groupId="frontend-lang">-<TabItem value="ts" label="TypeScript">-```tsx- const threadListManager = useThreadListManager({- fetchThreadList: async () => {- const response = await fetch("/api/threads");- const data = await response.json();- return data.threads.map((thread: Thread) => ({- threadId: +thread.threadId,- title: thread.title,- createdAt: new Date(thread.createdAt),- isRunning: false,- }));- },- createThread: (firstMessage) => {- return fetch("/api/threads", {- method: "POST",- body: JSON.stringify({- title: firstMessage.message!,- }),- }).then((res) => res.json());- },- deleteThread: async () => {}, // Called when the user deletes a thread. Use your delete thread API here- updateThread: async (t) => t, // Called when the user makes changes to an existing thread. Use your update thread API here- onSwitchToNew: () => {}, // Called when the user switches to a new thread.- onSelectThread: () => {}, // Called when the user selects an existing thread.- });-```-</TabItem>-<TabItem value="js" label="JavaScript">-```jsx- const threadListManager = useThreadListManager({- fetchThreadList: async () => {- const response = await fetch("/api/threads");- const data = await response.json();- return data.threads.map((thread) => ({- threadId: Number(thread.threadId),- title: thread.title,- createdAt: new Date(thread.createdAt),- isRunning: false,- }));- },- createThread: (firstMessage) => {- return fetch("/api/threads", {- method: "POST",- body: JSON.stringify({- title: firstMessage.message,- }),- }).then((res) => res.json());- },- deleteThread: async () => {}, // Called when the user deletes a thread. Use your delete API here- updateThread: async (t) => t, // Called when the user makes changes to an existing thread. Use your update thread API here- onSwitchToNew: () => {}, // Called when the user switches threads. This can be used to clear the chat when switching between threads- onSelectThread: () => {}, // Called when the user selects an existing thread.- });-```-</TabItem>-</Tabs>--## Managing data inside a thread--The `useThreadManager` hook lets you handle the data and events inside a thread. Here's an example of how you can manage the selected thread. The `threadListManager` returned by `useThreadListManager` makes things easier by providing a few things you might need.--<Tabs groupId="frontend-lang">-<TabItem value="ts" label="TypeScript">-```tsx- const threadManager = useThreadManager({- threadId: threadListManager.selectedThreadId,- shouldResetThreadState: threadListManager.shouldResetThreadState,- loadThread: async () => {- // Called to load the data for this specific thread. In this case, you would use your backend API that returns the selected thread's messages- const response = await fetch(- `/api/messages?threadId=${threadListManager.selectedThreadId}`- );- const data = await response.json();- return data.messages;- },- onProcessMessage: async ({ message, threadManager, abortController }) => {- // This is called when the user posts a message. Here, you might want to call your backend API that pushes messages inside the selected thread.- threadManager.appendMessages({- id: crypto.randomUUID(),- role: "user",- type: "prompt",- message: message.message as string,- });-- let threadId = threadListManager.selectedThreadId;- // If this is a new thread that currently has not been created on the backend, create a thread, otherwise push message to an existing thread- if (!threadId) {- const thread = await threadListManager.createThread(- message as UserMessage- );- threadListManager.selectThread(thread.threadId!, false);- threadId = thread.threadId;- }- const response = await fetch("/api/ask", {- method: "POST",- body: JSON.stringify({ threadId, ...message }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });-- // This is a utility function provided by crayon to make handling streamed messages more convenient.- await processStreamedMessage({- response,- createMessage: threadManager.appendMessages,- updateMessage: threadManager.updateMessage,- deleteMessage: (messageId) => {- const newMessages = threadManager.messages.filter(- (message) => message.id !== messageId- );- threadManager.setMessages(newMessages);- },- });- return [];- },- responseTemplates: templates,- });-```-</TabItem>-<TabItem value="js" label="JavaScript">-```jsx- const threadManager = useThreadManager({- threadId: threadListManager.selectedThreadId,- shouldResetThreadState: threadListManager.shouldResetThreadState,- loadThread: async () => {- // Called to load the data for this specific thread. In this case, you would use your backend API that returns the selected thread's messages- const response = await fetch(- `/api/messages?threadId=${threadListManager.selectedThreadId}`- );- const data = await response.json();- return data.messages;- },- onProcessMessage: async ({ message, threadManager, abortController }) => {- // This is called when the user posts a message. Here, you might want to call your backend API that pushes messages inside the selected thread.- threadManager.appendMessages({- id: crypto.randomUUID(),- role: "user",- type: "prompt",- message: message.message,- });-- let threadId = threadListManager.selectedThreadId;- // If this is a new thread that currently has not been created on the backend, create a thread, otherwise push message to an existing thread- if (!threadId) {- const thread = await threadListManager.createThread(message);- threadListManager.selectThread(thread.threadId, false);- threadId = thread.threadId;- }- const response = await fetch("/api/ask", {- method: "POST",- body: JSON.stringify({ threadId, ...message }),- headers: {- "Content-Type": "application/json",- Accept: "text/event-stream",- },- signal: abortController.signal,- });-- // This is a utility function provided by crayon to make handling streamed messages more convenient.- await processStreamedMessage({- response,- createMessage: threadManager.appendMessages,- updateMessage: threadManager.updateMessage,- deleteMessage: (messageId) => {- const newMessages = threadManager.messages.filter(- (message) => message.id !== messageId- );- threadManager.setMessages(newMessages);- },- });- return [];- },- responseTemplates: templates,- });-```-</TabItem>-</Tabs>-------The `threadListManager` and `threadManager` can then be passed to multiple crayon components such as the `ChatProvider` and `CrayonChat` to customize them to your needs. This enables usage of the Crayon UI with any type of backend.diff --git a/docs/docs/index.md b/docs/docs/index.mdxsimilarity index 93%rename from docs/docs/index.mdrename to docs/docs/index.mdxindex 303759a7c..1f0456cff 100644--- a/docs/docs/index.md+++ b/docs/docs/index.mdx@@ -1,6 +1,10 @@-# Crayon Documentation+import Admonition from "@theme/Admonition";-> **Crayon is a UI toolkit for building AI agents.**+# Introduction++<Admonition type="info" title={null} icon={null}>+ **Crayon is a UI toolkit for building AI agents.**+</Admonition>## What is Crayon?@@ -8,14 +12,17 @@ Crayon is a lightweight UI toolkit for building AI agents that can be integratedIt is composed of two libraries that can be used independently but are designed to work together seamlessly to create engaging and functional Agents.### Core+The core is a React based library that abstracts away the state management and backend integration.It is extremely lightweight and can be integrated with any HTTP server including but not limited to [LangChain](https://www.langchain.com/), [CrewAI](https://www.crewai.com/) or a simple [FastAPI](https://fastapi.tiangolo.com/) server serving a LLM driven agent.### Component Library+The component library is a set of beautifully designed & accessible UI components that are designed to work together to provide a live & responsive end-user experience for your AI applications. <br />It is built on top of headless Radix components and the Shell bridges the core and this component library to provide a seamless developer experience.## Generative UI & Crayon+While Crayon is agnostic to the use case, one of the key reasons we made it is to power Generative UI interfaces. <br />We believe that Generative UI represents the next paradigm shift in how we interact with AI and we want to make it easier for everyone to build them. See [Generative UI](/docs/concepts/generative-ui) for more details.@@ -25,9 +32,7 @@ Agents, Chatbots, Copilots - pretty much any application that is powered by an LIts designed to be extremely lightweight and work with any backend framework - [LangChain](https://www.langchain.com/), [CrewAI](https://www.crewai.com/) or a simple [FastAPI](https://fastapi.tiangolo.com/) serverintegrated with [OpenAI](https://openai.com/), [Anthropic](https://www.anthropic.com/), [Google Gemini](https://deepmind.google/technologies/gemini/), etc.-To get an idea of what you can build with Crayon, check out the example [Personal Finance Assistant](/docs/tutorials/Fino) we built with it.--<!-- TODO: Insert a link to the live app or gif once we have it. -->+{/* TODO: Insert a link to the live app or gif once we have it. */}## Features@@ -37,19 +42,21 @@ To get an idea of what you can build with Crayon, check out the example [Persona- **Extremely Lightweight**: Crayon core is designed to be extremely lightweight and doesn't add any overhead to your application beyond a few simple libraries.- **Themeable, Accessible & Responsive** - Crayon components are designed to be themeable, accessible & responsive across all surfaces and all interaction patterns - Chatbots, Agents, Copilots, etc.-## FAQs### Why did we build Crayon?+We believe that the future of AI is not just about creating more powerful LLMs, but also about creating more engaging and accessible applications that can be used by everyone. <br />We want to make it easier for developers to build AI applications and we believe that the best way to do this is to create a set of tools that can be adapted into any existing backend framework.### Why a new component library?+We started with off the shelf component libraries but realized that they needed some adaptationsto work within the context of a responsive chat interface. Since these components are used bothwithin the [Thesys Canvas](https://app.thesys.dev) & shipped to the end user, we decided to buildour version based on the powerful [Radix UI](https://www.radix-ui.com/primitives) headless library.### How does this relate to Thesys Canvas?+Crayon is a [MIT licensed](https://github.com/thesysdev/crayon/blob/main/LICENSE) UI library independent of Thesys Canvas meant to be used by anyone building AI agents. <br />[Thesys Canvas](https://app.thesys.dev) is a AI powered design and prototyping tool that allows you to create your own AI agents. The codegen feature on the canvas is powered by Crayon Component Library but Crayon has no dependency on Canvas itself.diff --git a/docs/docs/reference/js/react-core/interfaces/ResponseTemplate.md b/docs/docs/reference/js/react-core/interfaces/ResponseTemplate.mdindex 2264226a9..fea50dca0 100644--- a/docs/docs/reference/js/react-core/interfaces/ResponseTemplate.md+++ b/docs/docs/reference/js/react-core/interfaces/ResponseTemplate.md@@ -1,6 +1,6 @@A type that contains information about the name of the response template and the React component required to render the response template with that name.For detailed information on response templates, see [Core Concepts](../../../../concepts/001-core-concepts.md#response-templates), and for a guide on how to implement and use response templates,-see [Creating Response Templates](../../../../guides/customization/007-response-templates.mdx).+see [Creating Response Templates](../../../../guides/customization/005-response-templates.mdx).---diff --git a/docs/docs/tutorials/Fino/001-types.md b/docs/docs/tutorials/Fino/001-types.mddeleted file mode 100644index 679021740..000000000--- a/docs/docs/tutorials/Fino/001-types.md+++ /dev/null@@ -1,55 +0,0 @@-# Interface definitions--Before we begin writing any code, we'd need to setup the interface for our backend to UI communication.-communication.-In a traditional setup this would be defined via an OpenAPI spec or GraphQL schema but-since in an conversational setup there'd only be a single request and response, we'd need to define the-request and response types in a way that's easy to use in the frontend and can be be easily converted-to JSON Schema so that we can pass it to an LLM.--```typescript title="src/types/reponseTemplates/templates.ts"-export const BreakdownExpensesSummarySchema = z.object({- expenses: z.array(- z.object({- category: z.string(),- amount: z.number(),- })- ),- total_spent: z.number(),-});--export type BreakdownExpensesSummaryProps = z.infer<typeof BreakdownExpensesSummarySchema>;--export const templates = [- {- name: "breakdown_expenses",- description: "Renders a summary of the user's financial situation.",- parameters: BreakdownExpensesSummarySchema,- },-];--export const TemplatesJsonSchema = {- type: "object",- properties: {- response: {- type: "array",- items: {- oneOf: [- TextResponseSchema, // A simple text response for when no template is needed- ...templates.map((template) => ({- type: "object",- description: template.description,- properties: {- type: { const: "template" },- name: { const: template.name },- templateProps: zodToJsonSchema(template.parameters),- },- required: ["name", "templateProps", "type"],- additionalProperties: false,- })),- ],- },- },- },-} as const;-```diff --git a/docs/docs/tutorials/Fino/002-database.md b/docs/docs/tutorials/Fino/002-database.mddeleted file mode 100644index 7d3c533fe..000000000--- a/docs/docs/tutorials/Fino/002-database.md+++ /dev/null@@ -1,111 +0,0 @@-# Database Setup--In the real world, we'd want to use a database to store the user's personal finance history.--For this tutorial, we'll use a local SQLite database.--## Creating the database--```typescript title="src/lib/db/index.ts"-import { PrismaClient } from "@prisma/client";-import { initializeDb } from "./init";--declare global {- // eslint-disable-next-line no-var- var prisma: PrismaClient | undefined;-}--const prisma = global.prisma || new PrismaClient();--if (process.env.NODE_ENV !== "production") {- global.prisma = prisma;-}--// Initialize the database when this module is imported-(async () => {- try {- await initializeDb(prisma);- } catch (error) {- console.error("Failed to initialize database:", error);- }-})();--// Handle cleanup when the Node process is terminated-process.on("beforeExit", async () => {- await prisma?.$disconnect();-});--export default prisma;-```--## Filling it up with mock data-Since we don't have a real database, we'll need to fill it up with mock data.--```typescript title="src/lib/db/init.ts"-import { PrismaClient } from "@prisma/client";-import { randomInt } from "crypto";--export async function initializeDb(prisma: PrismaClient) {- // Check if there are any existing transactions- const existingTransaction = await prisma.transaction.findFirst();-- if (!existingTransaction) {- const debitCategories = [- "Food",- "Transportation",- "Entertainment",- "Utilities",- "Healthcare",- "Shopping",- "Education",- ];-- const creditCategories = [- "Salary",- "Investment",- "Refund",- "Gift",- "Bonus",- ];-- let balance = 0;- // Generate transactions with approximately 5:1 ratio of debits to credits- const transactions = Array.from({ length: 100 }, (_, i) => {- const transactionType = i % 6 === 0 ? "credit" : "debit";- const amount = Number((Math.random() * 490 + 10).toFixed(2)); // Random amount between 10 and 500, rounded to 2 decimal places- const finalAmount = transactionType === "credit" ? amount : -amount;- balance += finalAmount;-- const category =- transactionType === "credit"- ? creditCategories[- Math.floor(Math.random() * creditCategories.length)- ]- : debitCategories[Math.floor(Math.random() * debitCategories.length)];-- const date = new Date();- date.setDate(date.getDate() - randomInt(1, 365)); // Random date within the last year-- return {- date,- amount: finalAmount,- balance,- category,- description: "Mock transaction",- transaction_type: transactionType,- };- });-- // Create all transactions in a single transaction- await prisma.transaction.createMany({- data: transactions,- });- }-}-```--## Check that the database is setup correctly--```sh-npx prisma studio-```diff --git a/docs/docs/tutorials/Fino/003-backend.md b/docs/docs/tutorials/Fino/003-backend.mddeleted file mode 100644index 52469453d..000000000--- a/docs/docs/tutorials/Fino/003-backend.md+++ /dev/null@@ -1,107 +0,0 @@-# Chat Endpoint--## System Prompt-Add a system prompt to the assistant that will help it answer questions about the user's personal finance history.--```typescript title="src/api/chat/route.ts"-const SYSTEM_MESSAGE: ChatCompletionMessageParam = {- role: "system",- content:- "You are Fino, an AI financial advisor. " +- "You help users understand their finances, analyze transactions, and provide personalized financial advice. " +- "Be concise, professional, and focus on providing actionable insights. " +- "IMPORTANT: Your response must ALWAYS follow this structure:\n" +- "1. Start with an explanatory text string in the response array\n" +- "2. Follow with the appropriate template object\n" +- "3. End with additional insights or recommendations as text string\n" +- "You have access to tools to analyze transaction data. " +- "Use these tools when appropriate to provide data-driven insights. " +- "Always respond with a single JSON object containing a 'response' array which contains text or template object " +- "Example response structure:\n" +- '{"response": [\n' +- ' {"name": "breakdown_expenses", "templateProps": {...},\n' +- ' "Based on this breakdown, your highest spending is in Food category. Consider setting a budget to reduce these expenses."\n' +- "]}\n"-};-```--## Add data execution tool-Add a SQL execution tool to the assistant that will allow it to analyze the user's personal finance history.--```typescript title="src/api/chat/route.ts"-import { z } from "zod";--export const SQLArgsSchema = z.object({- query: z- .string()- .describe(- "SQL query to execute on the Transaction table. Use the Transaction table. " +- "quote the table name. "- ),- description: z- .string()- .describe(- "Human readable description of the intent of this tool call to be shown to the user. Do not include any sensitive information or PII. Construct the sentence in present participle tense."- ),-});--type SQLArgs = z.infer<typeof SQLArgsSchema>;--export async function execute_sql(args: SQLArgs): Promise<string> {- const result = await prisma.$queryRawUnsafe(args.query);- return JSON.stringify(result);-}-```--## Connecting the LLM--We pass the tools along with the templates to OpenAI.-Tools are functions that the LLM can call to get data whereas templates-represent the possible responses that the LLM can return along with their parameters.--Templates are defined in the `templates.ts` file and are passed to OpenAI as a JSON Schema-by using the `response_format` option. This way the LLM can choose the appropriate template-to use based on the user's query.--```typescript title="src/api/chat/route.ts"-import { TemplatesJsonSchema } from "@/types/reponseTemplates/templates";--const completion = openai.beta.chat.completions.runTools({- model: "gpt-4o",- messages: [SYSTEM_MESSAGE, ...historicalMessages, userMessage],- temperature: 0.5,- max_tokens: 1000,- stream: true,- tools: [- {- type: "function",- function: {- function: execute_sql,- description:- "Execute SQL lite queries on the Transaction table. \n" +- `id Int @id @default(autoincrement())- date DateTime- amount Float- balance Float- category String- transaction_type "credit" | "debit"`- .split("\n")- .map((line) => line.trim())- .join("\n"),- parameters: zodToJsonSchema(SQLArgsSchema) as object,- parse: (params: string) => {- return SQLArgsSchema.parse(JSON.parse(params));- },- strict: true,- },- },- ],- response_format: {- type: "json_schema",- json_schema: {- name: "json_schema",- schema: TemplatesJsonSchema,- },- },- });-```diff --git a/docs/docs/tutorials/Fino/004-frontend.md b/docs/docs/tutorials/Fino/004-frontend.mddeleted file mode 100644index ce7064b21..000000000--- a/docs/docs/tutorials/Fino/004-frontend.md+++ /dev/null@@ -1,44 +0,0 @@-# Generative UI--## Creating a response template for breakdown_expense-Now that we have a response template, we need to create a component that will render it.--```tsx title="src/app/reponseTemplates/templates.tsx"-"use client";-import { Card, CardHeader } from "@crayonai/react-ui";-import { PieChart } from "@crayonai/react-ui/Charts/PieChart";-import { BreakdownExpensesSummaryProps } from "@/types/responseTemplates/templates";--export const BreakdownExpenses: React.FC<BreakdownExpensesSummaryProps> = ({- expenses,- total_spent: totalSpent,-}) => {- const chartData = expenses.map((expense) => ({- category: expense.category,- amount: expense.amount,- }));-- return (- <Card className="items-center">- <CardHeader title={`Breakdown of expenses, Total Spent: ${totalSpent}`} />- <div className="w-[400px]">- <PieChart data={chartData} categoryKey="category" dataKey="amount" />- </div>- </Card>- );-};-```--## Passing response templates to CrayonChat-Lastly, we need to pass the response templates to the CrayonChat component so that it can render them.--```tsx title="src/app/page.tsx"-import { BreakdownExpenses } from "@/app/reponseTemplates/templates";--export default function Home() {- return <CrayonChat processMessage={processMessage} templates={[{- name: "breakdown_expenses",- Component: BreakdownExpenses,- }]} />;-}-```diff --git a/docs/docs/tutorials/Fino/index.md b/docs/docs/tutorials/Fino/index.mddeleted file mode 100644index 55b43b027..000000000--- a/docs/docs/tutorials/Fino/index.md+++ /dev/null@@ -1,24 +0,0 @@-----sidebar_position: 1------# Personal Finance Assistant--For this tutorial, we'll build a personal finance assistant that helps the user manage their finances.--<!-- TODO: Add a gif or link for the live app -->--## Goals-- User should be able to chat with the assistant and ask questions about their personal finance history.-- Answers should be well designed and interactive rather than overloading the user with a lot of text.-- User should be able to create and manage multiple chat threads.--## Setup--Follow the [Getting Started](/docs/quickstart/nextjs) guide to set up your project.--## Next Steps-- Add a system prompt to the assistant that will help it answer questions about the user's personal finance history.-- Add a SQL execution tool to the assistant that will allow it to analyze the user's personal finance history.-- Add a template to the assistant that will allow it to render the user's personal finance history in a structured format.-- Render the template in the frontend based on the parameters returned by the LLM.diff --git a/docs/sidebars.ts b/docs/sidebars.tsindex 735efa82d..f2a63de64 100644--- a/docs/sidebars.ts+++ b/docs/sidebars.ts@@ -33,9 +33,9 @@ const sidebars: SidebarsConfig = {items: [{ type: "autogenerated", dirName: "guides" }],},{- type: "category",- label: "Tutorials",- items: [{ type: "autogenerated", dirName: "tutorials" }],+ type: "doc",+ label: "Examples",+ id: "examples",},],referenceSidebar: [diff --git a/docs/src/sections/Hero.js b/docs/src/sections/Hero.jsindex ae6a381de..c8c7fd1a5 100644--- a/docs/src/sections/Hero.js+++ b/docs/src/sections/Hero.js@@ -40,9 +40,6 @@ export default function Hero() {<a href="/docs/quickstart/nextjs"><PrimaryButton>Get Started</PrimaryButton></a>- <a href="/docs/tutorials/Fino/">- <SecondaryButton>Tutorial</SecondaryButton>- </a></div><div className={styles.ctaBuild}>~ npm install @crayonai/react-core <br />