Skip to content

Commit bde5096

Browse files
authored
Merge pull request #1 from dayhaysoos/example
Example
2 parents 9a471fb + 9b8739b commit bde5096

29 files changed

+4683
-73
lines changed

convex/component/_generated/api.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
import type * as chat from "../chat.js";
1212
import type * as client from "../client.js";
1313
import type * as conversations from "../conversations.js";
14-
import type * as hello from "../hello.js";
1514
import type * as messages from "../messages.js";
1615
import type * as schemaTools from "../schemaTools.js";
1716
import type * as stream from "../stream.js";
18-
import type * as test from "../test.js";
1917
import type * as tools from "../tools.js";
2018

2119
import type {
@@ -29,11 +27,9 @@ const fullApi: ApiFromModules<{
2927
chat: typeof chat;
3028
client: typeof client;
3129
conversations: typeof conversations;
32-
hello: typeof hello;
3330
messages: typeof messages;
3431
schemaTools: typeof schemaTools;
3532
stream: typeof stream;
36-
test: typeof test;
3733
tools: typeof tools;
3834
}> = anyApi as any;
3935

convex/component/hello.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

convex/component/test.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
.env.local

example/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# E-commerce Database Chat Demo
2+
3+
A simple demo showing how to use the `@dayhaysoo/convex-database-chat` component to query an e-commerce product database using natural language.
4+
5+
## Features
6+
7+
- 🛒 50 mock e-commerce products across 4 categories
8+
- 💬 Natural language queries powered by Claude via OpenRouter
9+
- ⚡ Real-time streaming responses
10+
- 🔍 Search by name, category, price range
11+
- 📊 Get inventory statistics and low-stock alerts
12+
13+
## Setup
14+
15+
### 1. Install dependencies
16+
17+
```bash
18+
npm install
19+
```
20+
21+
### 2. Set up Convex
22+
23+
```bash
24+
npx convex dev
25+
```
26+
27+
This will prompt you to create a new Convex project and set up your environment.
28+
29+
### 3. Add OpenRouter API Key
30+
31+
Get an API key from [OpenRouter](https://openrouter.ai/) and add it to your Convex environment:
32+
33+
```bash
34+
npx convex env set OPENROUTER_API_KEY your_key_here
35+
```
36+
37+
### 4. Seed the database
38+
39+
```bash
40+
npm run seed
41+
```
42+
43+
This populates the database with 50 mock products.
44+
45+
### 5. Run the app
46+
47+
```bash
48+
npm run dev
49+
```
50+
51+
Open [http://localhost:5173](http://localhost:5173) in your browser.
52+
53+
## Example Queries
54+
55+
Try asking:
56+
57+
- "Show me all electronics under $50"
58+
- "What products are low on stock?"
59+
- "Give me an inventory overview"
60+
- "Find running shoes"
61+
- "How many products do we have in each category?"
62+
- "What's the most expensive item in sports?"
63+
64+
## Project Structure
65+
66+
```
67+
example/
68+
├── src/
69+
│ ├── main.tsx # App entry with ConvexProvider
70+
│ ├── App.tsx # Main app component
71+
│ ├── App.css # Styles
72+
│ └── components/
73+
│ └── Chat.tsx # Chat UI component
74+
└── convex/
75+
├── convex.config.ts # Mounts databaseChat component
76+
├── schema.ts # Products table schema
77+
├── seed.ts # Mock product data
78+
├── chatTools.ts # Query functions for LLM
79+
└── chat.ts # Chat integration
80+
```
81+
82+
## How It Works
83+
84+
1. **User asks a question** → Sent to Convex action
85+
2. **Action calls OpenRouter** → LLM with tool definitions
86+
3. **LLM decides to call tools** → e.g., `searchProducts({ category: "electronics" })`
87+
4. **Tool executes Convex query** → Returns product data
88+
5. **LLM formats response** → With markdown links to products
89+
6. **Response streamed to UI** → Real-time updates via Convex subscriptions

example/convex/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Welcome to your Convex functions directory!
2+
3+
Write your Convex functions here.
4+
See https://docs.convex.dev/functions for more.
5+
6+
A query function that takes two arguments looks like:
7+
8+
```ts
9+
// convex/myFunctions.ts
10+
import { query } from "./_generated/server";
11+
import { v } from "convex/values";
12+
13+
export const myQueryFunction = query({
14+
// Validators for arguments.
15+
args: {
16+
first: v.number(),
17+
second: v.string(),
18+
},
19+
20+
// Function implementation.
21+
handler: async (ctx, args) => {
22+
// Read the database as many times as you need here.
23+
// See https://docs.convex.dev/database/reading-data.
24+
const documents = await ctx.db.query("tablename").collect();
25+
26+
// Arguments passed from the client are properties of the args object.
27+
console.log(args.first, args.second);
28+
29+
// Write arbitrary JavaScript here: filter, aggregate, build derived data,
30+
// remove non-public properties, or create new objects.
31+
return documents;
32+
},
33+
});
34+
```
35+
36+
Using this query function in a React component looks like:
37+
38+
```ts
39+
const data = useQuery(api.myFunctions.myQueryFunction, {
40+
first: 10,
41+
second: "hello",
42+
});
43+
```
44+
45+
A mutation function looks like:
46+
47+
```ts
48+
// convex/myFunctions.ts
49+
import { mutation } from "./_generated/server";
50+
import { v } from "convex/values";
51+
52+
export const myMutationFunction = mutation({
53+
// Validators for arguments.
54+
args: {
55+
first: v.string(),
56+
second: v.string(),
57+
},
58+
59+
// Function implementation.
60+
handler: async (ctx, args) => {
61+
// Insert or modify documents in the database here.
62+
// Mutations can also read from the database like queries.
63+
// See https://docs.convex.dev/database/writing-data.
64+
const message = { body: args.first, author: args.second };
65+
const id = await ctx.db.insert("messages", message);
66+
67+
// Optionally, return a value from your mutation.
68+
return await ctx.db.get("messages", id);
69+
},
70+
});
71+
```
72+
73+
Using this mutation function in a React component looks like:
74+
75+
```ts
76+
const mutation = useMutation(api.myFunctions.myMutationFunction);
77+
function handleButtonPress() {
78+
// fire and forget, the most common way to use mutations
79+
mutation({ first: "Hello!", second: "me" });
80+
// OR
81+
// use the result once the mutation has completed
82+
mutation({ first: "Hello!", second: "me" }).then((result) =>
83+
console.log(result),
84+
);
85+
}
86+
```
87+
88+
Use the Convex CLI to push your functions to a deployment. See everything
89+
the Convex CLI can do by running `npx convex -h` in your project root
90+
directory. To learn more, launch the docs with `npx convex docs`.

0 commit comments

Comments
 (0)