forked from get-convex/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBasic.tsx
More file actions
215 lines (203 loc) · 7.18 KB
/
Copy pathChatBasic.tsx
File metadata and controls
215 lines (203 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { useMutation, useQuery } from "convex/react";
import { Toaster } from "../components/ui/toaster";
import { usePaginatedQuery } from "convex-helpers/react";
import { api } from "../../convex/_generated/api";
import {
optimisticallySendMessage,
toUIMessages,
useThreadMessages,
type UIMessage,
} from "@convex-dev/agent/react";
import { useCallback, useEffect, useState } from "react";
import { cn } from "../lib/utils";
function getThreadIdFromHash() {
return window.location.hash.replace(/^#/, "") || undefined;
}
export default function ChatBasic() {
const createThread = useMutation(api.threads.createNewThread);
const [threadId, setThreadId] = useState<string | undefined>(
typeof window !== "undefined" ? getThreadIdFromHash() : undefined,
);
// Fetch thread title if threadId exists
const threadDetails = useQuery(
api.threads.getThreadDetails,
threadId ? { threadId } : "skip",
);
// Fetch all threads (internal API)
// For demo, hardcode userId as in backend
const threads = usePaginatedQuery(
api.threads.listThreads,
{},
{ initialNumItems: 20 },
);
// Listen for hash changes
useEffect(() => {
function onHashChange() {
setThreadId(getThreadIdFromHash());
}
window.addEventListener("hashchange", onHashChange);
return () => window.removeEventListener("hashchange", onHashChange);
}, []);
// Reset handler: create a new thread and update hash
const newThread = useCallback(() => {
void createThread({ title: "Fresh thread" }).then((newId) => {
window.location.hash = newId;
setThreadId(newId);
});
}, [createThread]);
// On mount or when threadId changes, if no threadId, create one and set hash
useEffect(() => {
if (!threadId) newThread();
}, [newThread, threadId]);
return (
<div className="h-full flex flex-col">
<header className="bg-white/80 backdrop-blur-sm p-4 flex justify-between items-center border-b">
<div className="flex items-center gap-4">
<h1 className="text-xl font-semibold accent-text">
Basic Chat Example
</h1>
{threadId && threadDetails && threadDetails.title && (
<span
className="text-gray-500 text-base font-normal truncate max-w-xs"
title={threadDetails.title}
>
— {threadDetails.title}
</span>
)}
</div>
</header>
<div className="h-full flex flex-row bg-gray-50 flex-1 min-h-0">
{/* Sidebar */}
<aside className="w-64 bg-white border-r flex flex-col h-full min-h-0">
<div className="p-4 border-b font-semibold text-lg">Threads</div>
<div className="flex-1 overflow-y-auto min-h-0">
{threads.results.length === 0 && (
<div className="p-4 text-gray-400 text-sm">No threads yet.</div>
)}
<ul>
{threads.results.map((thread) => (
<li key={thread._id}>
<button
className={cn(
"w-full text-left px-4 py-2 hover:bg-blue-50 transition flex items-center gap-2",
threadId === thread._id &&
"bg-blue-100 text-blue-900 font-semibold",
)}
onClick={() => {
window.location.hash = thread._id;
setThreadId(thread._id);
}}
>
<span className="truncate max-w-[10rem]">
{thread.title || "Untitled thread"}
</span>
</button>
</li>
))}
</ul>
</div>
<div className="px-4 py-2">
<button
onClick={newThread}
className="w-full flex justify-center items-center gap-2 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition font-semibold shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-400"
type="button"
>
<span className="text-lg">+</span>
<span>New Thread</span>
</button>
</div>
</aside>
{/* Main chat area */}
<main className="flex-1 flex flex-col items-center justify-center p-8 h-full min-h-0">
{threadId ? (
<Chat threadId={threadId} />
) : (
<div className="text-center text-gray-500">Loading...</div>
)}
</main>
<Toaster />
</div>
</div>
);
}
function Chat({ threadId }: { threadId: string }) {
const messages = useThreadMessages(
api.chat.basic.listMessages,
{ threadId },
{ initialNumItems: 10 },
);
const sendMessage = useMutation(
api.chat.basic.sendMessage,
).withOptimisticUpdate(
optimisticallySendMessage(api.chat.basic.listMessages),
);
const [prompt, setPrompt] = useState("Yo yo yo");
function onSendClicked() {
const trimmedPrompt = prompt.trim();
if (trimmedPrompt === "") return;
void sendMessage({ threadId, prompt: trimmedPrompt }).catch(() =>
setPrompt(prompt),
);
setPrompt("");
}
return (
<>
<div className="w-full max-w-2xl bg-white rounded-xl shadow-lg p-6 flex flex-col gap-6 h-full min-h-0 justify-end">
{messages.status !== "Exhausted" && messages.results?.length > 0 && (
<div className="flex justify-center">
<button
className="px-4 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition font-semibold disabled:opacity-50"
onClick={() => messages.loadMore(10)}
disabled={messages.status !== "CanLoadMore"}
>
Load More
</button>
</div>
)}
{messages.results?.length > 0 && (
<div className="flex flex-col gap-4 overflow-y-auto mb-4 flex-1 min-h-0">
{toUIMessages(messages.results ?? []).map((m) => (
<Message key={m.key} message={m} />
))}
</div>
)}
<form
className="flex gap-2 items-center"
onSubmit={(e) => {
e.preventDefault();
onSendClicked();
}}
>
<input
type="text"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
className="flex-1 px-4 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-400 bg-gray-50"
placeholder="Ask me anything..."
/>
<button
type="submit"
className="px-4 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition font-semibold disabled:opacity-50"
disabled={!prompt.trim()}
>
Send
</button>
</form>
</div>
</>
);
}
function Message({ message }: { message: UIMessage }) {
const isUser = message.role === "user";
return (
<div className={`flex ${isUser ? "justify-end" : "justify-start"}`}>
<div
className={`rounded-lg px-4 py-2 max-w-lg whitespace-pre-wrap shadow-sm ${
isUser ? "bg-blue-100 text-blue-900" : "bg-gray-200 text-gray-800"
}`}
>
{message.content}
</div>
</div>
);
}