From dc8b2b177c31a0301c3c5655c2041e2ab33d58ad Mon Sep 17 00:00:00 2001 From: ali Date: Mon, 5 Aug 2024 23:18:42 +0200 Subject: [PATCH 01/30] added env example file --- .../online-store-customer-service/online-cs-chatbot/.env.example | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/online-store-customer-service/online-cs-chatbot/.env.example diff --git a/examples/online-store-customer-service/online-cs-chatbot/.env.example b/examples/online-store-customer-service/online-cs-chatbot/.env.example new file mode 100644 index 00000000..85d27328 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/.env.example @@ -0,0 +1 @@ +NEXT_LB_PIPE_API_KEY="" \ No newline at end of file From eb3764a5d33a836a952f3dd592cf47a14c3f7525 Mon Sep 17 00:00:00 2001 From: ali Date: Mon, 5 Aug 2024 23:20:28 +0200 Subject: [PATCH 02/30] gitignore front and backend for online-store-customer-service --- .../online-cs-chatbot-backend/.gitignore | 183 ++++++++++++++++++ .../online-cs-chatbot/.gitignore | 54 ++++++ 2 files changed, 237 insertions(+) create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore create mode 100644 examples/online-store-customer-service/online-cs-chatbot/.gitignore diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore b/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore new file mode 100644 index 00000000..e0773208 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore @@ -0,0 +1,183 @@ +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# wrangler project + +.dev.vars +.wrangler/ + +.vscode + + +# No lock files. +package-lock.json +yarn.lock + + + +.editorconfig diff --git a/examples/online-store-customer-service/online-cs-chatbot/.gitignore b/examples/online-store-customer-service/online-cs-chatbot/.gitignore new file mode 100644 index 00000000..54a02eee --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/.gitignore @@ -0,0 +1,54 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env +.env*.local +.copy.local.env +.copy.remote.env + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts + +# Supabase +seed.sql +xseed.sql +xxseed.sql +-seed.sql +/supabase/seed.sql +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ + +# No lock files. +package-lock.json +yarn.lock +dist From 553580ab35f14231e9e5a20c29cab4a421fd6fdd Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:01:35 +0200 Subject: [PATCH 03/30] added dev vars example file --- examples/online-store-customer-service/.dev.vars.example | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 examples/online-store-customer-service/.dev.vars.example diff --git a/examples/online-store-customer-service/.dev.vars.example b/examples/online-store-customer-service/.dev.vars.example new file mode 100644 index 00000000..60a0c0b1 --- /dev/null +++ b/examples/online-store-customer-service/.dev.vars.example @@ -0,0 +1,4 @@ +LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY="" +LANGBASE_TRAVEL_PIPE_API_KEY="" +LANGBASE_ELECTRONICS_PIPE_API_KEY="" +LANGBASE_SPORTS_PIPE_API_KEY="" \ No newline at end of file From 2785986c30bad03a06a6774d72190ecc7fe8fe80 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:21:07 +0200 Subject: [PATCH 04/30] updated readme for online-store-customer-service --- .../online-cs-chatbot/README.md | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/README.md diff --git a/examples/online-store-customer-service/online-cs-chatbot/README.md b/examples/online-store-customer-service/online-cs-chatbot/README.md new file mode 100755 index 00000000..0adcd379 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/README.md @@ -0,0 +1,97 @@ +![Demo Online Store (TechBay) Customer Service Chatbot by ⌘ Langbase][cover] + +![License: MIT][mit] [![Fork to ⌘ Langbase][fork]][pipe-main-chatbot] + +## Build an Online Store (TechBay) Customer Service Chatbot with Pipes β€” ⌘ Langbase + +This chatbot is built by using an multiple AI Pipes (Chatbot agents) on Langbase, it works with 30+ LLMs (OpenAI, Gemini, Mistral, Llama, Gemma, etc), any Data (10M+ context with Memory sets), and any Framework (standard web API you can use with any software). + + +Check out the live demo [here][demo]. + +## Features + +- πŸ’¬ [Online Store (TechBay) Customer Service Chatbot][demo] β€” Built with an [AI Pipe on ⌘ Langbase][pipe-main-chatbot] +- ⚑️ Streaming β€” Real-time chat experience with streamed responses +- πŸ—£οΈ Q/A β€” Ask questions and get pre-defined answers with your preferred AI model and tone +- πŸ”‹ Responsive and open source β€” Works on all devices and platforms + + + +## Learn more + +1. Take a look at the main chatbot pipe that routes call for other departments in the online store [Online Store (TechBay) Customer Service Main Chatbot Pipe on ⌘ Langbase][pipe-main-chatbot] +2. Take a look at the electronics section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Electronics Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +3. Take a look at the sports gear section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Sports Gear Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +4. Take a look at the travel bags section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Travel Bags Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +2. Read the [source code on GitHub][gh] for this example +3. Go through Documentaion: [Pipe Quick Start][qs] +4. Learn more about [Pipes & Memory features on ⌘ Langbase][docs] + + +## Get started + +Let's get started with the project: + +To get started with Langbase, you'll need to [create a free personal account on Langbase.com][signup] and verify your email address. _Done? Cool, cool!_ + +1. Fork the [Online Store (TechBay) Customer Service Main Chatbot Pipe][pipe-main-chatbot] Pipe on ⌘ Langbase. +2. Fork the [Online Store (TechBay) Customer Service Sports Gear Department Agent Pipe][pipe-sports-gear] Pipe on ⌘ Langbase. +3. Fork the [Online Store (TechBay) Customer Service Electronics Department Agent Pipe][pipe-electronics-dept] Pipe on ⌘ Langbase. +4. Fork the [Online Store (TechBay) Customer Service Travel Bags Department Agent Pipe][pipe-travel-bags-dept] Pipe on ⌘ Langbase. +5. Go to the API tab to copy the Pipe's API key (to be used on server-side only). +6. Download the example project folder from [here][download] or clone the reppository. +7. `cd` into the online-cs-chatbot-backend directory and open it in your code editor. +8. Duplicate the `.dev.var.example` file in this project and rename it to `.dev.vars`. +9. Add the following environment variables (.dev.vars): +``` + # Replace `PIPE_API_KEY` with the copied API key. + LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY="PIPE_API_KEY" + LANGBASE_TRAVEL_PIPE_API_KEY="PIPE_API_KEY" + LANGBASE_ELECTRONICS_PIPE_API_KEY="PIPE_API_KEY" + LANGBASE_SPORTS_PIPE_API_KEY="PIPE_API_KEY" +``` + +10. Issue the following in your CLI separately for online-cs-chatbot directory (frontend of the chatbot) and online-cs-chatbot-backend directory (backend of the chatbot) : +```sh + # Install the dependencies using the following command: + npm install + + # Run the project using the following command: + npm run dev +``` + +11. Your app template (frontend of the chatbot) should now be running on [localhost:3000][localfrontend]. + +> NOTE: +> This is a Next.js project, so you can build and deploy it to any platform of your choice, like Vercel, Netlify, Cloudflare, etc. + +12. Your backend which is a cloudflare worker should now be running on [localhost:8787][localbackend]. + +> NOTE: +> This project requires cloudflare account so that cloudflare CLI [Cloudflare CLI account and setup][cloudflare] + +## Authors + +This project is created by [Langbase][lb] team members, with contributions from: + +- Muhammad-Ali Danish - Software Engineer, [Langbase][lb]
+**_Built by ⌘ [Langbase.com][lb] β€” Ship hyper-personalized AI assistants with memory!_** + +[cover]:https://raw.githubusercontent.com/LangbaseInc/docs-images/main/examples/online-store-customer-service/online-store-customer-service.png +[lb]: https://langbase.com +[demo]: https://online-store-customer-service.langbase.dev +[pipe-main-chatbot]: https://beta.langbase.com/examples/online-store-customer-service +[pipe-electronics-dept]: https://beta.langbase.com/examples/online-store-electronics-dept +[pipe-travel-bags-dept]: https://beta.langbase.com/examples/online-store-travel-bags-dept +[pipe-sports-gear]: https://beta.langbase.com/examples/online-store-sports-gear-dept +[gh]: https://github.com/LangbaseInc/langbase-examples/tree/main/examples/online-store-customer-service +[download]:https://download-directory.github.io/?url=https://github.com/LangbaseInc/langbase-examples/tree/main/examples/online-store-customer-service +[qs]:https://langbase.com/docs/pipe/quickstart +[docs]:https://langbase.com/docs +[local]:http://localhost:3000 +[mit]: https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge&color=%23000000 +[fork]: https://img.shields.io/badge/FORK%20ON-%E2%8C%98%20Langbase-000000.svg?style=for-the-badge&logo=%E2%8C%98%20Langbase&logoColor=000000 +[localfrontend]:http://localhost:3000 +[localbackend]:http://localhost:8787 +[cloudflare]:https://developers.cloudflare.com/workers/get-started/guide/ \ No newline at end of file From 2473fb65c7f591d8b72ea5b3045d844703c935d4 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:28:57 +0200 Subject: [PATCH 05/30] updated readme --- .../online-cs-chatbot/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/online-store-customer-service/online-cs-chatbot/README.md b/examples/online-store-customer-service/online-cs-chatbot/README.md index 0adcd379..10415245 100755 --- a/examples/online-store-customer-service/online-cs-chatbot/README.md +++ b/examples/online-store-customer-service/online-cs-chatbot/README.md @@ -71,6 +71,18 @@ To get started with Langbase, you'll need to [create a free personal account on > NOTE: > This project requires cloudflare account so that cloudflare CLI [Cloudflare CLI account and setup][cloudflare] +13. To test the classification and function call you can the following test dataset: +``` +Electronic Dept. Query: + I order 2 days ago a new Sony Bravia XR-65X90L, I did recieve confirmation that my order will be shipped shortly but I have not recieve any tracking number or any confirmation that the order has been disptachen. Please reply soon as I order because you guys ship fast and it should not take more then a week. + +Travel Bags Dept. Query: + I have received Wenger backpack for 15 inch laptop two days ago, however it quite small for my use case. I want return and order new Wenger 30L for 16inch laptop. Let me know if it is in stock? + +Sports Gear Dept. Query: + I bought Nike zoomx two days ago, although I received the order confirmation and the receipt but I have not received the tracking number. Please send the tracking number asap. +``` + ## Authors This project is created by [Langbase][lb] team members, with contributions from: From ed8276bdd3d7e40ee6d326307e5f7bf35c941f24 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:30:31 +0200 Subject: [PATCH 06/30] move dev vars file --- .../{ => online-cs-chatbot-backend}/.dev.vars.example | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/online-store-customer-service/{ => online-cs-chatbot-backend}/.dev.vars.example (100%) diff --git a/examples/online-store-customer-service/.dev.vars.example b/examples/online-store-customer-service/online-cs-chatbot-backend/.dev.vars.example similarity index 100% rename from examples/online-store-customer-service/.dev.vars.example rename to examples/online-store-customer-service/online-cs-chatbot-backend/.dev.vars.example From fef24ea77fd5ce79d0d42c49cf3c19409f8e7705 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:31:20 +0200 Subject: [PATCH 07/30] move readme file --- .../{online-cs-chatbot => }/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/online-store-customer-service/{online-cs-chatbot => }/README.md (100%) diff --git a/examples/online-store-customer-service/online-cs-chatbot/README.md b/examples/online-store-customer-service/README.md similarity index 100% rename from examples/online-store-customer-service/online-cs-chatbot/README.md rename to examples/online-store-customer-service/README.md From 3a2807aef4c1b571ad7651cba8d10511862ee6d2 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:33:48 +0200 Subject: [PATCH 08/30] updated opening comp for online-store-customer-service --- .../online-cs-chatbot/components/opening.tsx | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx new file mode 100755 index 00000000..9c28df98 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx @@ -0,0 +1,89 @@ +import Link from 'next/link' + +export function Opening() { + return ( +
+
+
+
+ + Chatbot Example + +
+ +
+
+

+ Online Store (TechBay) Customer Service Chatbot by a + + pipe on ⌘ Langbase + +

+
+ Ship hyper-personalized AI assistants with memory. +
+
+ +
+

Online Store (TechBay) Customer Service example consists of three pipes checkout the following links:

+
+ + 1. + Fork ticket resolution ChatBot for electronics section Pipe on ⌘ Langbase + + + 2. + Fork ticket resolution ChatBot for sports gear section Pipe on ⌘ Langbase + + + 3. + Fork ticket resolution ChatBot for travel bags section Pipe on ⌘ Langbase + + + 4. + Use LangUI.dev's open source code components + + + + 5. + Go through Documentaion: Pipe Quickstart + + + 6. + + Learn more about Pipes & Memory features on ⌘ Langbase + + +
+
+
+
+
+ ) +} + +// Description Link +function Dlink({ + href, + children, + ...props +}: { + href: string + children: React.ReactNode + [key: string]: any +}) { + return ( + + {children} + + ) +} From 112173cad717951df8e176492fccb7b5e73ad6e7 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:34:31 +0200 Subject: [PATCH 09/30] updated layout for online-store-customer-service --- .../online-cs-chatbot/app/layout.tsx | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx b/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx new file mode 100755 index 00000000..af058970 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx @@ -0,0 +1,36 @@ +import { Header } from '@/components/header' +import cn from 'mxcn' +import type { Metadata } from 'next' +import { Inter } from 'next/font/google' +import { Toaster } from 'sonner' +import './globals.css' + +const inter = Inter({ subsets: ['latin'] }) + +export const metadata: Metadata = { + title: 'Online Store Customer Service Chatbot - Langbase', + description: 'Build an Online Store Customer Service Chatbot with ⌘ Langbase using any LLM model.', + keywords: ['Online Store Customer Service', 'Chatbot', 'Langbase'] +} + +export default function RootLayout({ + children +}: Readonly<{ + children: React.ReactNode +}>) { + return ( + + +
+
+ +
+
+ {children} +
+
+
+ + + ) +} From d7f0a328ebec2723b231cc5defffafd79153ca01 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:35:55 +0200 Subject: [PATCH 10/30] updated route server comp for backend comm --- .../online-cs-chatbot/app/api/chat/route.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts b/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts new file mode 100755 index 00000000..0038a367 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts @@ -0,0 +1,43 @@ +import { OpenAIStream, StreamingTextResponse } from 'ai' + +export const runtime = 'edge' + +/** + * Stream AI Chat Messages from Langbase + * + * @param req + * @returns + */ + +export async function POST(req: Request) { + try { + const body = await req.json() + const { messages, threadId } = body + + const response = await fetch('http://localhost:8787', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'lb-thread-id': threadId || '' + }, + body: JSON.stringify({ messages: messages }) + }) + + // Create a stream from the response + const stream = OpenAIStream(response) + + // Return a streaming response + return new StreamingTextResponse(stream, { + headers: { + 'lb-thread-id': response.headers.get('lb-thread-id') || '' + } + }) + + + } catch (error: any) { + console.error('Uncaught API Error:', error) + return new Response(JSON.stringify({ error: error.message }), { + status: 500 + }) + } +} From eaf8a94222c08da8c4894ff4dd42083a59d7e769 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:36:36 +0200 Subject: [PATCH 11/30] updated header comp for online-store-customer-service --- .../online-cs-chatbot/components/header.tsx | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/header.tsx diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx new file mode 100755 index 00000000..d696362e --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx @@ -0,0 +1,47 @@ +import { buttonVariants } from '@/components/ui/button' +import cn from 'mxcn' +import Link from 'next/link' +import { IconFork, IconGitHub } from './ui/icons' + +export async function Header() { + return ( +
+
+

+ + + Langbase + +

+
+ + +
+ ) +} From 2429d304ab3b5aa5298b201ed6636d1b6739e116 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:37:10 +0200 Subject: [PATCH 12/30] updated package name for frontend online-store-customer-service --- .../online-cs-chatbot/package.json | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 examples/online-store-customer-service/online-cs-chatbot/package.json diff --git a/examples/online-store-customer-service/online-cs-chatbot/package.json b/examples/online-store-customer-service/online-cs-chatbot/package.json new file mode 100755 index 00000000..c869d000 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/package.json @@ -0,0 +1,49 @@ +{ + "name": "online-store-customer-service-example", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "@radix-ui/react-label": "^2.0.2", + "@radix-ui/react-separator": "^1.0.3", + "@radix-ui/react-slot": "^1.0.2", + "@radix-ui/react-switch": "^1.0.3", + "ai": "3.0.16", + "class-variance-authority": "^0.7.0", + "mxcn": "^2.0.0", + "next": "14.0.4", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-markdown": "^8.0.7", + "react-syntax-highlighter": "^15.5.0", + "react-textarea-autosize": "^8.4.1", + "remark-gfm": "^3.0.1", + "remark-math": "^5.1.1", + "sonner": "^1.5.0" + }, + "devDependencies": { + "@tailwindcss/typography": "^0.5.9", + "@types/node": "^17.0.12", + "@types/react": "^18.0.22", + "@types/react-dom": "^18.0.7", + "@types/react-syntax-highlighter": "^15.5.6", + "@typescript-eslint/parser": "^5.59.7", + "autoprefixer": "^10.4.13", + "eslint": "^8.31.0", + "eslint-config-next": "13.4.19", + "eslint-config-prettier": "^8.3.0", + "eslint-plugin-tailwindcss": "^3.12.0", + "postcss": "^8.4.21", + "prettier": "^3.0.3", + "prettier-plugin-tailwindcss": "^0.5.4", + "tailwind-merge": "^1.12.0", + "tailwindcss": "^3.4.1", + "tailwindcss-animate": "^1.0.5", + "typescript": "^5.2.2" + } +} From da2ce4e5e5b0d18c98dfb09b2c28111f83d574f8 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:38:35 +0200 Subject: [PATCH 13/30] Backend logic for main chatbot and agents --- .../online-cs-chatbot-backend/src/index.ts | 251 ++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts new file mode 100644 index 00000000..49b99d92 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts @@ -0,0 +1,251 @@ +interface LangbaseResponse { + completion: string; +} + +interface ToolCall { + id: string; + type: string; + function: { + name: string; + arguments: string; + }; +} + +interface AssistantMessage { + role: string; + content: string | null; + tool_calls?: ToolCall[]; +} + +interface Choice { + index: number; + message: AssistantMessage; + logprobs: null; + finish_reason: string; +} + +interface Usage { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; +} + +interface RawResponse { + id: string; + object: string; + created: number; + model: string; + choices: Choice[]; + usage: Usage; + system_fingerprint: null; +} + +interface ApiResponse { + success: boolean; + completion: string; + raw: RawResponse; +} + +export interface Env { + OPENAI_API_KEY: string; + LANGBASE_TRAVEL_PIPE_API_KEY: string, + LANGBASE_ELECTRONICS_PIPE_API_KEY: string, + LANGBASE_SPORTS_PIPE_API_KEY: string, + LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY: string +} + + +function getDepartmentKey(functionName: string): keyof Pick | null { + const departmentMap: { [key: string]: keyof Pick } = { + 'call_sports_dept': 'LANGBASE_SPORTS_PIPE_API_KEY', + 'call_electronics_dept': 'LANGBASE_ELECTRONICS_PIPE_API_KEY', + 'call_travel_dept': 'LANGBASE_TRAVEL_PIPE_API_KEY' + }; + return departmentMap[functionName] || null; +} + + +function createMessageStream(content: string): ReadableStream { + return new ReadableStream({ + start(controller) { + const encoder = new TextEncoder(); + controller.enqueue(encoder.encode('data: ' + JSON.stringify({ choices: [{ delta: { content } }] }) + '\n\n')); + controller.close(); + } + }); +} + +function createErrorStream(errorMessage: string): ReadableStream { + return createMessageStream(errorMessage); +} + +async function callDepartment(deptKey: keyof Pick, customerQuery: string, env: Env, threadId?: string): Promise { + console.log(`Calling ${deptKey} department with query:`, customerQuery); + + const response = await fetch('https://api.langbase.com/beta/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${env[deptKey]}`, + }, + body: JSON.stringify({ + messages: [{ role: 'user', content: customerQuery }], + ...(threadId && { threadId }) + }), + }); + + if (!response.ok) { + return new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode(`Error: ${response.status} ${response.statusText}`)); + controller.close(); + } + }); + } + + const reader = response.body!.getReader(); + const decoder = new TextDecoder(); + const encoder = new TextEncoder(); + + return new ReadableStream({ + async start(controller) { + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + const chunk = decoder.decode(value, { stream: true }); + controller.enqueue(encoder.encode(chunk)); + } + controller.close(); + } + }); +} + + +async function processDepartmentCalls(toolCalls: ToolCall[], env: Env, threadId: string): Promise { + for (const toolCall of toolCalls) { + const functionName = toolCall.function.name; + const functionArgs = JSON.parse(toolCall.function.arguments); + const departmentKey = getDepartmentKey(functionName); + if (departmentKey) { + return await callDepartment(departmentKey, functionArgs.customerQuery, env, threadId); + } + } + return createErrorStream('No valid department call found'); +} + + +async function processRawChoices(choice: Choice, env: Env, threadId: string): Promise { + const assistantMessage = choice.message; + if (assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0) { + return await processDepartmentCalls(assistantMessage.tool_calls, env, threadId); + } else { + const content = assistantMessage.content || 'Sorry, I couldn\'t process your request.'; + return createMessageStream(content); + } +} + +function processSuccessfulCompletion(completion: any): ReadableStream { + try { + const parsedCompletion = typeof completion === 'string' ? JSON.parse(completion) : completion; + const message = parsedCompletion.message || parsedCompletion.content || JSON.stringify(parsedCompletion); + return createMessageStream(message); + } catch (error) { + console.error('Error parsing completion:', error); + console.log('Raw completion:', completion); + return createErrorStream('Error processing response'); + } +} + + +async function processMainChatbotResponse(data: ApiResponse, env: Env, threadId: string): Promise { + if (data.success && data.completion) { + return processSuccessfulCompletion(data.completion); + } else if (data.raw && data.raw.choices && data.raw.choices.length > 0) { + return processRawChoices(data.raw.choices[0], env, threadId); + } else { + return createErrorStream('Unexpected response format'); + } +} + + +async function callMainChatbot(query: string, threadId: string | undefined, env: Env): Promise<{ data: ApiResponse, threadId: string | undefined }> { + const userQuery = { + threadId, + messages: [{ role: 'user', content: query }], + }; + const response = await fetch('https://api.langbase.com/beta/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${env['LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY']}`, + }, + body: JSON.stringify(userQuery), + }); + + const data = await response.json() as ApiResponse; + return { + data, + threadId: response.headers.get('lb-thread-id') || threadId + }; +} + + +export default { + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + if (request.method === 'OPTIONS') { + return new Response(null, { + headers: { + 'Access-Control-Allow-Origin': 'http://localhost:3000', + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + }, + }); + } + + if (request.method !== 'POST') { + return new Response('Method Not Allowed', { status: 405 }); + } + + let incomingMessages; + let threadId = request.headers.get('lb-thread-id') || undefined; + try { + const body = await request.json() as { messages: any[], threadId?: string }; + incomingMessages = body.messages; + threadId = body.threadId || threadId; + + if (!incomingMessages || !Array.isArray(incomingMessages) || incomingMessages.length === 0) { + throw new Error('Invalid or empty messages array'); + } + } catch (error) { + console.error('Error processing request:', (error as Error).message); + return new Response(JSON.stringify({ error: (error as Error).message }), { + status: 400, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': 'http://localhost:3000', + }, + }); + } + + const query = incomingMessages[incomingMessages.length - 1].content; + const { data, threadId: newThreadId } = await callMainChatbot(query, threadId, env); + threadId = newThreadId; + + const responseStream = await processMainChatbotResponse(data, env, threadId || ''); + + return new Response(responseStream, { + headers: { + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache', + 'Connection': 'keep-alive', + 'Access-Control-Allow-Origin': 'http://localhost:3000', + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, lb-thread-id', + 'lb-thread-id': threadId || '', + }, + }); + }, +}; + + From 1f5c753fd1bda671b6a6da412e6f2a5357a689cc Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:40:14 +0200 Subject: [PATCH 14/30] all backend wrangler worker files --- .../online-cs-chatbot-backend/.prettierrc | 6 + .../online-cs-chatbot-backend/package.json | 26 +++++ .../test/index.spec.ts | 25 ++++ .../test/tsconfig.json | 8 ++ .../online-cs-chatbot-backend/tsconfig.json | 104 +++++++++++++++++ .../vitest.config.mts | 11 ++ .../worker-configuration.d.ts | 4 + .../online-cs-chatbot-backend/wrangler.toml | 108 ++++++++++++++++++ 8 files changed, 292 insertions(+) create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/package.json create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts create mode 100644 examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc b/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc new file mode 100644 index 00000000..5c7b5d3c --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc @@ -0,0 +1,6 @@ +{ + "printWidth": 140, + "singleQuote": true, + "semi": true, + "useTabs": true +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/package.json b/examples/online-store-customer-service/online-cs-chatbot-backend/package.json new file mode 100644 index 00000000..5673f52f --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/package.json @@ -0,0 +1,26 @@ +{ + "name": "summer-flower-b680", + "version": "0.0.0", + "private": true, + "scripts": { + "deploy": "wrangler deploy", + "dev": "wrangler dev", + "start": "wrangler dev", + "test": "vitest", + "build": "wrangler build", + "cf-typegen": "wrangler types" + }, + "devDependencies": { + "@cloudflare/vitest-pool-workers": "^0.4.5", + "@cloudflare/workers-types": "^4.20240620.0", + "typescript": "^5.4.5", + "vitest": "1.5.0", + "wrangler": "^3.60.3" + }, + "dependencies": { + "ai": "^3.0.16", + "cheerio": "^1.0.0-rc.12", + "langbase": "^0.2.5", + "openai": "^4.52.0" + } +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts new file mode 100644 index 00000000..fbee335d --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts @@ -0,0 +1,25 @@ +// test/index.spec.ts +import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test'; +import { describe, it, expect } from 'vitest'; +import worker from '../src/index'; + +// For now, you'll need to do something like this to get a correctly-typed +// `Request` to pass to `worker.fetch()`. +const IncomingRequest = Request; + +describe('Hello World worker', () => { + it('responds with Hello World! (unit style)', async () => { + const request = new IncomingRequest('http://example.com'); + // Create an empty context to pass to `worker.fetch()`. + const ctx = createExecutionContext(); + const response = await worker.fetch(request, env, ctx); + // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions + await waitOnExecutionContext(ctx); + expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + }); + + it('responds with Hello World! (integration style)', async () => { + const response = await SELF.fetch('https://example.com'); + expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + }); +}); diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json b/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json new file mode 100644 index 00000000..bc019a7e --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"] + }, + "include": ["./**/*.ts", "../src/env.d.ts"], + "exclude": [] +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json b/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json new file mode 100644 index 00000000..132ca912 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json @@ -0,0 +1,104 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Projects */ + // "incremental": true, /* Enable incremental compilation */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + "lib": ["es2021"] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, + "jsx": "react" /* Specify what JSX code is generated. */, + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ + // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + + /* Modules */ + "module": "es2022" /* Specify what module code is generated. */, + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */, + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ + "types": [ + "@cloudflare/workers-types/2023-07-01" + ] /* Specify type package names to be included without being referenced in a source file. */, + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + "resolveJsonModule": true /* Enable importing .json files */, + // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */, + "checkJs": true /* Enable error reporting in type-checked JavaScript files. */, + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + "noEmit": true /* Disable emitting files from a compilation. */, + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */, + "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, + // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ + // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ + // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "exclude": ["test"] +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts b/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts new file mode 100644 index 00000000..503a7174 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts @@ -0,0 +1,11 @@ +import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'; + +export default defineWorkersConfig({ + test: { + poolOptions: { + workers: { + wrangler: { configPath: './wrangler.toml' }, + }, + }, + }, +}); diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts new file mode 100644 index 00000000..5b2319b3 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts @@ -0,0 +1,4 @@ +// Generated by Wrangler +// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen` +interface Env { +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml b/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml new file mode 100644 index 00000000..934a09ff --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml @@ -0,0 +1,108 @@ +#:schema node_modules/wrangler/config-schema.json +name = "summer-flower-b680" +main = "src/index.ts" +compatibility_date = "2024-06-20" +compatibility_flags = ["nodejs_compat"] + +# Automatically place your workloads in an optimal location to minimize latency. +# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure +# rather than the end user may result in better performance. +# Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement +# [placement] +# mode = "smart" + +# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) +# Docs: +# - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables +# Note: Use secrets to store sensitive data. +# - https://developers.cloudflare.com/workers/configuration/secrets/ +# [vars] +# MY_VARIABLE = "production_value" + +# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai +# [ai] +# binding = "AI" + +# Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets +# [[analytics_engine_datasets]] +# binding = "MY_DATASET" + +# Bind a headless browser instance running on Cloudflare's global network. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering +# [browser] +# binding = "MY_BROWSER" + +# Bind a D1 database. D1 is Cloudflare’s native serverless SQL database. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases +# [[d1_databases]] +# binding = "MY_DB" +# database_name = "my-database" +# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +# Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms +# [[dispatch_namespaces]] +# binding = "MY_DISPATCHER" +# namespace = "my-namespace" + +# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. +# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects +# [[durable_objects.bindings]] +# name = "MY_DURABLE_OBJECT" +# class_name = "MyDurableObject" + +# Durable Object migrations. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations +# [[migrations]] +# tag = "v1" +# new_classes = ["MyDurableObject"] + +# Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive +# [[hyperdrive]] +# binding = "MY_HYPERDRIVE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind an mTLS certificate. Use to present a client certificate when communicating with another service. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates +# [[mtls_certificates]] +# binding = "MY_CERTIFICATE" +# certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues +# [[queues.producers]] +# binding = "MY_QUEUE" +# queue = "my-queue" + +# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues +# [[queues.consumers]] +# queue = "my-queue" + +# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets +# [[r2_buckets]] +# binding = "MY_BUCKET" +# bucket_name = "my-bucket" + +# Bind another Worker service. Use this binding to call another Worker without network overhead. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings +# [[services]] +# binding = "MY_SERVICE" +# service = "my-service" + +# Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes +# [[vectorize]] +# binding = "MY_INDEX" +# index_name = "my-index" From ad34a652eda0e8ce393d8c965cc6f58f6b654d73 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 6 Aug 2024 01:40:30 +0200 Subject: [PATCH 15/30] nextjs chat ui --- .../online-cs-chatbot/app/favicon.ico | Bin 0 -> 15086 bytes .../online-cs-chatbot/app/globals.css | 99 +++++ .../online-cs-chatbot/app/page.tsx | 7 + .../components/chat-input.tsx | 75 ++++ .../components/chat-list.tsx | 27 ++ .../components/chat-message-actions.tsx | 40 +++ .../components/chat-message.tsx | 77 ++++ .../components/chatbot-page.tsx | 57 +++ .../online-cs-chatbot/components/markdown.tsx | 9 + .../components/prompt-form.tsx | 95 +++++ .../components/ui/button.tsx | 70 ++++ .../components/ui/codeblock.tsx | 145 ++++++++ .../online-cs-chatbot/components/ui/icons.tsx | 339 ++++++++++++++++++ .../components/ui/separator.tsx | 32 ++ .../components/ui/textarea.tsx | 23 ++ .../lib/hooks/use-copy-to-clipboard.tsx | 33 ++ .../lib/hooks/use-enter-submit.tsx | 23 ++ .../online-cs-chatbot/lib/types.ts | 11 + .../online-cs-chatbot/lib/utils.ts | 8 + .../online-cs-chatbot/next.config.js | 13 + .../online-cs-chatbot/postcss.config.js | 6 + .../online-cs-chatbot/prettier.config.cjs | 34 ++ .../online-cs-chatbot/public/chatbot.jpg | Bin 0 -> 318121 bytes .../online-cs-chatbot/tailwind.config.ts | 177 +++++++++ .../online-cs-chatbot/tsconfig.json | 29 ++ 25 files changed, 1429 insertions(+) create mode 100644 examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico create mode 100755 examples/online-store-customer-service/online-cs-chatbot/app/globals.css create mode 100755 examples/online-store-customer-service/online-cs-chatbot/app/page.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/ui/button.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/ui/codeblock.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/ui/icons.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/ui/separator.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/components/ui/textarea.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/lib/hooks/use-copy-to-clipboard.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/lib/hooks/use-enter-submit.tsx create mode 100755 examples/online-store-customer-service/online-cs-chatbot/lib/types.ts create mode 100755 examples/online-store-customer-service/online-cs-chatbot/lib/utils.ts create mode 100755 examples/online-store-customer-service/online-cs-chatbot/next.config.js create mode 100755 examples/online-store-customer-service/online-cs-chatbot/postcss.config.js create mode 100755 examples/online-store-customer-service/online-cs-chatbot/prettier.config.cjs create mode 100644 examples/online-store-customer-service/online-cs-chatbot/public/chatbot.jpg create mode 100755 examples/online-store-customer-service/online-cs-chatbot/tailwind.config.ts create mode 100755 examples/online-store-customer-service/online-cs-chatbot/tsconfig.json diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico b/examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..7c6d4a8ab54ca2d2c0415c9a405185a1305dcc9d GIT binary patch literal 15086 zcmdU#M~oCn7KXE7p|L~b&1H}PF(9#k@J={k1;X4A-g_@V2q7A`5(5_&??JdZfMBoK z0))Bj1qmcy7dp|6jp9gkROxahYz1K{Y(BcNfvuuS8LFs@QcUT)QeiQslE((N zu!-%$B%KYMF-pbR=0WwrCbq$lPAaED3zXg}1=%R>d~AaOECoqrl^MAYO2v&am3}aQ z1x!&B{Z^9g?ynPA#zq9+RryN7-~E{V|G@;du*AG|Udb=}SNRXNu(_4Fm-XbYg4d4& zY+y{5SoF3HWCHzgK}$^7~ArACb!fjZKW3BUwa@7hNE6uIr|)TtxKj~|yGKYqyf@89L~ z=g;o@;lqcediCl*)dB3sjvaIPyfTLl9ZD(_fF0dn1+z`WfIry`%3HQ9B+ zzkYqWefze&fB#E%VB>YuC=r$7YWnJ><)mFLM3* zb*WjiW5uMml@;Y(k$|*^nog?Vc||XNZj9TfTg`v6-8jD^sRSkv47GNXwQjWz3i{ zW-Q;oe_wj{?k%lawKCMVZ(n)%@S%M8@Igk57-4vknK^T&JbCgYaLlB_q2+eJS|I#& z>(-T%Cr=u?-@bj5ckkYrwgsP^lWZPs&O7rIW$=)P|B(kBJ$f{B-VLb-`{`#P<$?90 z4$MQ0WqbpA{P?lKh(G7#efS-g|Z_$>r> z>J9eNjz4Yxq3wvQG*SIwM0qkjw zUvMp2v`8*oxM0>R)+~IS`6MqdPpAWN9mIHGOjAZZ;DyegKQD9V&Mj!{;~g(xXAbh_ zrF{3sV$6xGxvX2nWF&CfJo0&m4}!?f9%+?hL>2w8}p?RzkwO-X~z$&z217IGyfDKezWld zF;J1mZ{GOF8^5(}+cu%jMG(IcgA$7-=neW`ty(p?cI}$+bNt22$5qAL`JdgUp^i+r(mk#3xi|0@*R+m6<+w=H! z`2O4di)Nd>SmgbqH-2OOU_Rjq1;t|QIZvE8Vdfye%#kBU3L3xp_m2hMzk`Q8A^R%k zdiL=P7cLBq#nO@SwYlw6|Nfmm;7lTNAE1}mix`al;2b4NES8Q8wmbwz&U%ui(tLhJ zeM+ApqDY?O*r4OHlNCBDZB zvlmu|EIQCtkknq8FDc;$Vfv$#p#xp$jFNgP(=DYax(mXCPIQMQFcybxixb^J?LZ%0 zRIID)jhx9;s8GSL&)l(g>eR{b zG-=XAkWrmMge`267@ymelJc@IpDfC z9?pVBjT)6ypOuN*#CCI~98Z3N9Ow1ir|;jtU$~>?3@J$EpQLbD$DPO+4@NLBD)2Ge71w7o>zj*QDr+e@u zcCZ~f-nqNqwQHBWdGkgtUc6}R(Ei-do;r2P5Z^c87hvGL3f|3|HygZ3Y2;xqt9(}S;jYQIo~ym9z8nDHf5}N*e0c1e_4ly4jn42vB}n7+MaoT$dDmstuDa&i~W+0 zKfq9u@fY?&tWGedrr9YG&E3tk-ypu;6Jjqf$&HSg* z8>Jv0q6{8*y@~(-QVNpu=fi7tl-GB3x;)A5C6nYKgDiD=W#!phWM^l~v}x1Ky34)( z^y$-O#flZus8J)=MqCEjqgIyZ%9Sh49%=2`wbH9sFX_{#kE~w3+I%aC4li$9_K`P_ zwHE%}yLX%QaLbl0vUBH7S+i!1!Q|!DcVrV{4B`mv5?ifbzg{+M*dSZCZk3)rdnSCN z5Z7b-*TqGB`Njg7b?ep{9bo9*y}KKScrYNJfS>&gScxCev2EKnQy1F9|NU`XHlh7i zty(1`M~*Z)_;$;-AM1WV`z7=ru?c-ZpRsmRA289U0sU84|Ium~Urg73ktY>q{N!uM iADgsCq!uoSl(RR+;~4%i%=^+5Z8{p@M$^ literal 0 HcmV?d00001 diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/globals.css b/examples/online-store-customer-service/online-cs-chatbot/app/globals.css new file mode 100755 index 00000000..36084b74 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/globals.css @@ -0,0 +1,99 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +/* Zinc */ +/* --background: 240 10% 3.9%; */ +/* --muted: 240 3.7% 15.9%; */ +@layer base { + :root { + --background: 0 0% 100%; + --foreground: 240 10% 3.9%; + --card: 0 0% 100%; + --card-foreground: 240 10% 3.9%; + --popover: 0 0% 100%; + --popover-foreground: 240 10% 3.9%; + --primary: 240 5.9% 10%; + --primary-foreground: 0 0% 98%; + --secondary: 240 4.8% 95.9%; + --secondary-foreground: 240 5.9% 10%; + --muted: 240 4.8% 95.9%; + --muted-foreground: 240 3.8% 46.1%; + --accent: 240 4.8% 95.9%; + --accent-foreground: 240 5.9% 10%; + /* --destructive: 0 84.2% 60.2%; */ + --destructive: 2.74 92.59% 62.94%; + --destructive-foreground: 0 0% 98%; + --warning: 46.38 70.61% 48.04%; + --warning-foreground: 120 12.5% 3.14%; + --border: 240 5.9% 90%; + --input: 240 5.9% 90%; + --ring: 240 5.9% 10%; + --radius: 6px; + --danger: 2.74 92.59% 62.94%; + } + + .dark { + /* --background: 120 12.5% 3.14%; */ + --background: 240, 3%, 9%; + --foreground: 0 0% 98%; + --card: 240 10% 3.9%; + --card-foreground: 0 0% 98%; + --popover: 240 10% 3.9%; + --popover-foreground: 0 0% 98%; + --primary: 0 0% 98%; + --primary-foreground: 240 5.9% 10%; + --secondary: 240 3.7% 15.9%; + --secondary-foreground: 0 0% 98%; + /* --muted: 165 10% 7.84%; */ + --muted: 240 3.45% 11.37%; + --muted-foreground: 240 5% 64.9%; + --accent: 240 3.7% 15.9%; + --accent-foreground: 0 0% 98%; + /* --destructive: 0 62.8% 30.6%; */ + --destructive: 356.18 70.61% 48.04%; + --destructive-foreground: 0 0% 98%; + --warning: 46.38 70.61% 48.04%; + --warning-foreground: 120 12.5% 3.14%; + /* --border: 240 3.7% 15.9%; */ + --border: 240 2% 14%; + --border-muted: 240 2% 14%; + --input: 240 3.7% 15.9%; + --ring: 240 4.9% 83.9%; + --danger: 356.18 70.61% 48.04%; + } +} + +@layer base { + * { + @apply border-border; + } + body { + @apply bg-background text-foreground; + } +} + +::selection { + color: hsl(var(--background)); + background: hsl(var(--foreground)); +} + +.google { + display: inline-block; + width: 20px; + height: 20px; + position: relative; + background-size: contain; + background-repeat: no-repeat; + background-position: 50%; + background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 48 48'%3E%3Cdefs%3E%3Cpath id='a' d='M44.5 20H24v8.5h11.8C34.7 33.9 30.1 37 24 37c-7.2 0-13-5.8-13-13s5.8-13 13-13c3.1 0 5.9 1.1 8.1 2.9l6.4-6.4C34.6 4.1 29.6 2 24 2 11.8 2 2 11.8 2 24s9.8 22 22 22c11 0 21-8 21-22 0-1.3-.2-2.7-.5-4z'/%3E%3C/defs%3E%3CclipPath id='b'%3E%3Cuse xlink:href='%23a' overflow='visible'/%3E%3C/clipPath%3E%3Cpath clip-path='url(%23b)' fill='%23FBBC05' d='M0 37V11l17 13z'/%3E%3Cpath clip-path='url(%23b)' fill='%23EA4335' d='M0 11l17 13 7-6.1L48 14V0H0z'/%3E%3Cpath clip-path='url(%23b)' fill='%2334A853' d='M0 37l30-23 7.9 1L48 0v48H0z'/%3E%3Cpath clip-path='url(%23b)' fill='%234285F4' d='M48 48L17 24l-4-3 35-10z'/%3E%3C/svg%3E"); +} + +@keyframes spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx b/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx new file mode 100755 index 00000000..f278d230 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx @@ -0,0 +1,7 @@ +import { Chatbot } from '@/components/chatbot-page' + +export const runtime = 'edge' + +export default function ChatPage() { + return +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx new file mode 100755 index 00000000..569fbc6f --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx @@ -0,0 +1,75 @@ +import { type UseChatHelpers } from 'ai/react' + +import { PromptForm } from '@/components/prompt-form' +import { Button } from '@/components/ui/button' +import { IconRegenerate, IconStop } from '@/components/ui/icons' + +export interface ChatInputProps + extends Pick< + UseChatHelpers, + | 'append' + | 'isLoading' + | 'reload' + | 'messages' + | 'stop' + | 'input' + | 'setInput' + > { + id?: string +} + +export function ChatInput({ + id, + isLoading, + stop, + append, + reload, + input, + setInput, + messages +}: ChatInputProps) { + return ( +
+
+
+ {isLoading ? ( + + ) : ( + messages?.length > 0 && ( + + ) + )} +
+
+ { + await append({ + content: value, + role: 'user' + }) + }} + input={input} + setInput={setInput} + isLoading={isLoading} + /> +
+
+
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx new file mode 100755 index 00000000..32074928 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx @@ -0,0 +1,27 @@ +import { type Message } from 'ai' + +import { Separator } from '@/components/ui/separator' +import { ChatMessage } from '@/components/chat-message' + +export interface ChatList { + messages: Message[] +} + +export function ChatList({ messages }: ChatList) { + if (!messages.length) { + return null + } + + return ( +
+ {messages.map((message, index) => ( +
+ + {index < messages.length - 1 && ( + + )} +
+ ))} +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx new file mode 100755 index 00000000..37f68b2a --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx @@ -0,0 +1,40 @@ +'use client' + +import { type Message } from 'ai' + +import { Button } from '@/components/ui/button' +import { IconCheck, IconCopy } from '@/components/ui/icons' +import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard' +import cn from 'mxcn' + +interface ChatMessageActionsProps extends React.ComponentProps<'div'> { + message: Message +} + +export function ChatMessageActions({ + message, + className, + ...props +}: ChatMessageActionsProps) { + const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 }) + + const onCopy = () => { + if (isCopied) return + copyToClipboard(message.content) + } + + return ( +
+ +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx new file mode 100755 index 00000000..c359618d --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx @@ -0,0 +1,77 @@ +import { Message } from 'ai' +import remarkGfm from 'remark-gfm' +import remarkMath from 'remark-math' + +import { ChatMessageActions } from '@/components/chat-message-actions' +import { MemoizedReactMarkdown } from '@/components/markdown' +import { CodeBlock } from '@/components/ui/codeblock' +import { IconSparkles, IconUser } from '@/components/ui/icons' +import cn from 'mxcn' + +export interface ChatMessageProps { + message: Message +} + +export function ChatMessage({ message, ...props }: ChatMessageProps) { + return ( +
+
+ {message.role === 'user' ? : } +
+
+ {children}

+ }, + code({ node, inline, className, children, ...props }) { + if (children.length) { + if (children[0] == '▍') { + return ( + ▍ + ) + } + + children[0] = (children[0] as string).replace('`▍`', '▍') + } + + const match = /language-(\w+)/.exec(className || '') + + if (inline) { + return ( + + {children} + + ) + } + + return ( + + ) + } + }} + > + {message.content} +
+ +
+
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx new file mode 100755 index 00000000..80203378 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx @@ -0,0 +1,57 @@ +'use client' + +import { ChatList } from '@/components/chat-list' +import { useChat, type Message } from 'ai/react' +import cn from 'mxcn' +import { useState } from 'react' +import { toast } from 'sonner' +import { ChatInput } from './chat-input' +import { Opening } from './opening' + +export interface ChatProps extends React.ComponentProps<'div'> { + id?: string // Optional: Thread ID if you want to persist the chat in a DB + initialMessages?: Message[] // Optional: Messages to pre-populate the chat from DB +} + +export function Chatbot({ id, initialMessages, className }: ChatProps) { + const [threadId, setThreadId] = useState(null) + const { messages, append, reload, stop, isLoading, input, setInput } = + useChat({ + api: '/api/chat', + initialMessages, + body: { threadId }, + onResponse(response) { + if (response.status !== 200) { + console.log('✨ ~ response:', response) + toast.error(response.statusText) + } + + // Get Thread ID from response header + const lbThreadId = response.headers.get('lb-thread-id') + setThreadId(lbThreadId) + } + }) + return ( +
+
+ {messages.length ? ( + <> + + + ) : ( + + )} +
+ +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx new file mode 100755 index 00000000..d4491467 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx @@ -0,0 +1,9 @@ +import { FC, memo } from 'react' +import ReactMarkdown, { Options } from 'react-markdown' + +export const MemoizedReactMarkdown: FC = memo( + ReactMarkdown, + (prevProps, nextProps) => + prevProps.children === nextProps.children && + prevProps.className === nextProps.className +) diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx new file mode 100755 index 00000000..49b6cb77 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx @@ -0,0 +1,95 @@ +import { Button } from '@/components/ui/button' +import { IconChat, IconCommand, IconSpinner } from '@/components/ui/icons' +import { useEnterSubmit } from '@/lib/hooks/use-enter-submit' +import { UseChatHelpers } from 'ai/react' +import * as React from 'react' +import Textarea from 'react-textarea-autosize' + +export interface PromptProps + extends Pick { + onSubmit: (value: string) => Promise + isLoading: boolean +} + +export function PromptForm({ + onSubmit, + input, + setInput, + isLoading +}: PromptProps) { + const { formRef, onKeyDown } = useEnterSubmit() + const inputRef = React.useRef(null) + + React.useEffect(() => { + if (inputRef.current) { + inputRef.current.focus() + } + }, []) + + return ( +
{ + e.preventDefault() + if (!input?.trim()) { + return + } + setInput('') + await onSubmit(input) + }} + ref={formRef} + > +
+
+ +
+