Skip to content

The Open Standard for Generative UI

Unbundle creating template from chat example (#275)

Rabi Shanker Guha0627528a44c0
diff --git a/eslint.config.cjs b/eslint.config.cjs
index 9056ae83c..22c907096 100644
--- a/eslint.config.cjs
+++ b/eslint.config.cjs
@@ -7,6 +7,9 @@ const eslintPluginPrettier = require("eslint-plugin-prettier");
const reactHooks = require("eslint-plugin-react-hooks");
module.exports = [
+ {
+ ignores: ["**/src/templates/**"],
+ },
{
files: ["**/__tests__/**/*.{ts,tsx}", "**/*.test.{ts,tsx}", "**/*.spec.{ts,tsx}"],
languageOptions: {
diff --git a/packages/create-openui-app/package.json b/packages/create-openui-app/package.json
index 7014b36ed..161ad9af8 100644
--- a/packages/create-openui-app/package.json
+++ b/packages/create-openui-app/package.json
@@ -10,13 +10,13 @@
],
"scripts": {
"build:cli": "tsc -p .",
- "build:templates": "mkdir -p dist/templates && rsync -a --delete --exclude='.next' --exclude='node_modules' ../../examples/openui-chat/ dist/templates/openui-chat",
+ "build:templates": "mkdir -p dist/templates && cp -r src/templates/openui-chat dist/templates/openui-chat",
"build": "pnpm run build:cli && pnpm run build:templates",
"build:exec": "node dist/index.js",
- "lint:check": "eslint ./src",
- "lint:fix": "eslint ./src --fix",
- "format:fix": "prettier --write ./src",
- "format:check": "prettier --check ./src",
+ "lint:check": "eslint ./src --ignore-pattern 'src/templates/**'",
+ "lint:fix": "eslint ./src --fix --ignore-pattern 'src/templates/**'",
+ "format:fix": "prettier --write './src/**' '!./src/templates/**'",
+ "format:check": "prettier --check './src/**' '!./src/templates/**'",
"ci": "pnpm run lint:check && pnpm run format:check"
},
"devDependencies": {
diff --git a/packages/create-openui-app/src/templates/openui-chat/.gitignore b/packages/create-openui-app/src/templates/openui-chat/.gitignore
new file mode 100644
index 000000000..5ef6a5207
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/.gitignore
@@ -0,0 +1,41 @@
+# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
+
+# dependencies
+/node_modules
+/.pnp
+.pnp.*
+.yarn/*
+!.yarn/patches
+!.yarn/plugins
+!.yarn/releases
+!.yarn/versions
+
+# testing
+/coverage
+
+# next.js
+/.next/
+/out/
+
+# production
+/build
+
+# misc
+.DS_Store
+*.pem
+
+# debug
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+.pnpm-debug.log*
+
+# env files (can opt-in for committing if needed)
+.env*
+
+# vercel
+.vercel
+
+# typescript
+*.tsbuildinfo
+next-env.d.ts
diff --git a/packages/create-openui-app/src/templates/openui-chat/README.md b/packages/create-openui-app/src/templates/openui-chat/README.md
new file mode 100644
index 000000000..78fd7b43a
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/README.md
@@ -0,0 +1,27 @@
+This is an [OpenUI](https://openui.com) Agent Chat project bootstrapped with [`create-openui-app`](https://openui.com/docs/chat/quick-start).
+
+## Getting Started
+
+First, run the development server:
+
+```bash
+npm run dev
+# or
+yarn dev
+# or
+pnpm dev
+# or
+bun dev
+```
+
+Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
+
+You can start editing the page by modifying `src/app/api/route.ts` and improving your agent
+by adding system prompts or tools.
+
+## Learn More
+
+To learn more about OpenUI, take a look at the following resources:
+
+- [OpenUI Documentation](https://openui.com/docs) - learn about OpenUI features and API.
+- [OpenUI GitHub repository](https://github.com/thesysdev/openui) - your feedback and contributions are welcome!
diff --git a/packages/create-openui-app/src/templates/openui-chat/eslint.config.mjs b/packages/create-openui-app/src/templates/openui-chat/eslint.config.mjs
new file mode 100644
index 000000000..05e726d1b
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/eslint.config.mjs
@@ -0,0 +1,18 @@
+import { defineConfig, globalIgnores } from "eslint/config";
+import nextVitals from "eslint-config-next/core-web-vitals";
+import nextTs from "eslint-config-next/typescript";
+
+const eslintConfig = defineConfig([
+ ...nextVitals,
+ ...nextTs,
+ // Override default ignores of eslint-config-next.
+ globalIgnores([
+ // Default ignores of eslint-config-next:
+ ".next/**",
+ "out/**",
+ "build/**",
+ "next-env.d.ts",
+ ]),
+]);
+
+export default eslintConfig;
diff --git a/packages/create-openui-app/src/templates/openui-chat/next.config.ts b/packages/create-openui-app/src/templates/openui-chat/next.config.ts
new file mode 100644
index 000000000..cb651cdc0
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/next.config.ts
@@ -0,0 +1,5 @@
+import type { NextConfig } from "next";
+
+const nextConfig: NextConfig = {};
+
+export default nextConfig;
diff --git a/packages/create-openui-app/src/templates/openui-chat/package.json b/packages/create-openui-app/src/templates/openui-chat/package.json
new file mode 100644
index 000000000..5f7811b9f
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/package.json
@@ -0,0 +1,30 @@
+{
+ "name": "openui-chat",
+ "version": "0.1.0",
+ "private": true,
+ "scripts": {
+ "dev": "next dev",
+ "build": "next build",
+ "start": "next start",
+ "lint": "eslint"
+ },
+ "dependencies": {
+ "@openuidev/react-ui": "latest",
+ "@openuidev/react-headless": "latest",
+ "@openuidev/lang-react": "latest",
+ "next": "16.1.6",
+ "openai": "^6.22.0",
+ "react": "19.2.3",
+ "react-dom": "19.2.3"
+ },
+ "devDependencies": {
+ "@tailwindcss/postcss": "^4",
+ "@types/node": "^20",
+ "@types/react": "^19",
+ "@types/react-dom": "^19",
+ "eslint": "^9",
+ "eslint-config-next": "16.1.6",
+ "tailwindcss": "^4",
+ "typescript": "^5"
+ }
+}
diff --git a/packages/create-openui-app/src/templates/openui-chat/postcss.config.mjs b/packages/create-openui-app/src/templates/openui-chat/postcss.config.mjs
new file mode 100644
index 000000000..61e36849c
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/postcss.config.mjs
@@ -0,0 +1,7 @@
+const config = {
+ plugins: {
+ "@tailwindcss/postcss": {},
+ },
+};
+
+export default config;
diff --git a/packages/create-openui-app/src/templates/openui-chat/src/app/api/chat/route.ts b/packages/create-openui-app/src/templates/openui-chat/src/app/api/chat/route.ts
new file mode 100644
index 000000000..404fd0276
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/src/app/api/chat/route.ts
@@ -0,0 +1,35 @@
+import { NextRequest } from "next/server";
+import OpenAI from "openai";
+
+export async function POST(req: NextRequest) {
+ const client = new OpenAI();
+ try {
+ const { messages, systemPrompt } = await req.json();
+
+ const chatMessages: OpenAI.ChatCompletionMessageParam[] = [
+ ...(systemPrompt ? [{ role: "system" as const, content: systemPrompt }] : []),
+ ...messages,
+ ];
+
+ const response = await client.chat.completions.create({
+ model: "gpt-4o",
+ messages: chatMessages,
+ stream: true,
+ });
+
+ return new Response(response.toReadableStream(), {
+ headers: {
+ "Content-Type": "text/event-stream",
+ "Cache-Control": "no-cache, no-transform",
+ Connection: "keep-alive",
+ },
+ });
+ } catch (err) {
+ console.error(err);
+ const message = err instanceof Error ? err.message : "Unknown error";
+ return new Response(JSON.stringify({ error: message }), {
+ status: 500,
+ headers: { "Content-Type": "application/json" },
+ });
+ }
+}
diff --git a/packages/create-openui-app/src/templates/openui-chat/src/app/globals.css b/packages/create-openui-app/src/templates/openui-chat/src/app/globals.css
new file mode 100644
index 000000000..f1d8c73cd
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/src/app/globals.css
@@ -0,0 +1 @@
+@import "tailwindcss";
diff --git a/packages/create-openui-app/src/templates/openui-chat/src/app/layout.tsx b/packages/create-openui-app/src/templates/openui-chat/src/app/layout.tsx
new file mode 100644
index 000000000..a7fbfb1a4
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/src/app/layout.tsx
@@ -0,0 +1,19 @@
+import type { Metadata } from "next";
+import "./globals.css";
+
+export const metadata: Metadata = {
+ title: "OpenUI Chat",
+ description: "Generative UI Chat with OpenAI SDK",
+};
+
+export default function RootLayout({
+ children,
+}: Readonly<{
+ children: React.ReactNode;
+}>) {
+ return (
+ <html lang="en">
+ <body>{children}</body>
+ </html>
+ );
+}
diff --git a/packages/create-openui-app/src/templates/openui-chat/src/app/page.tsx b/packages/create-openui-app/src/templates/openui-chat/src/app/page.tsx
new file mode 100644
index 000000000..db66d864a
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/src/app/page.tsx
@@ -0,0 +1,32 @@
+"use client";
+import "@openuidev/react-ui/components.css";
+import "@openuidev/react-ui/styles/index.css";
+
+import { openAIMessageFormat, openAIReadableStreamAdapter } from "@openuidev/react-headless";
+import { FullScreen } from "@openuidev/react-ui";
+import { defaultLibrary, defaultPromptOptions } from "@openuidev/react-ui/genui-lib";
+
+const systemPrompt = defaultLibrary.prompt(defaultPromptOptions);
+
+export default function Home() {
+ return (
+ <div className="h-screen w-screen overflow-hidden">
+ <FullScreen
+ processMessage={async ({ messages, abortController }) => {
+ return fetch("/api/chat", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ messages: openAIMessageFormat.toApi(messages),
+ systemPrompt,
+ }),
+ signal: abortController.signal,
+ });
+ }}
+ streamProtocol={openAIReadableStreamAdapter()}
+ componentLibrary={defaultLibrary}
+ agentName="OpenUI Chat"
+ />
+ </div>
+ );
+}
diff --git a/packages/create-openui-app/src/templates/openui-chat/tsconfig.json b/packages/create-openui-app/src/templates/openui-chat/tsconfig.json
new file mode 100644
index 000000000..cf9c65d3e
--- /dev/null
+++ b/packages/create-openui-app/src/templates/openui-chat/tsconfig.json
@@ -0,0 +1,34 @@
+{
+ "compilerOptions": {
+ "target": "ES2017",
+ "lib": ["dom", "dom.iterable", "esnext"],
+ "allowJs": true,
+ "skipLibCheck": true,
+ "strict": true,
+ "noEmit": true,
+ "esModuleInterop": true,
+ "module": "esnext",
+ "moduleResolution": "bundler",
+ "resolveJsonModule": true,
+ "isolatedModules": true,
+ "jsx": "react-jsx",
+ "incremental": true,
+ "plugins": [
+ {
+ "name": "next"
+ }
+ ],
+ "paths": {
+ "@/*": ["./src/*"]
+ }
+ },
+ "include": [
+ "next-env.d.ts",
+ "**/*.ts",
+ "**/*.tsx",
+ ".next/types/**/*.ts",
+ ".next/dev/types/**/*.ts",
+ "**/*.mts"
+ ],
+ "exclude": ["node_modules"]
+}
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index abe40a0f3..69f3ece14 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -2,3 +2,4 @@ packages:
- "packages/*"
- "examples/*"
- "docs/"
+ - "!**/src/templates/**"