xonecas.io
5414
Sa

sat / commit 1637b79

feat: vibe coded chat without persistance

xonecas2026-01-14 1637b79d1dd150df4e775e8aa5fce5092c5cfdf0 parent 346869d

9 files changed

src/app/api/stream/route.ts+25−0
@@ -0,0 +1,25 @@
1+import { NextResponse } from "next/server";
2+import { chat, type StreamEvent } from "@/lib/chat";
3+
4+export async function POST(request: Request) {
5+ const { userInput } = await request.json();
6+
7+ const encoder = new TextEncoder();
8+ const stream = new ReadableStream({
9+ async start(controller) {
10+ await chat(userInput, (event: StreamEvent) => {
11+ // Use newline-delimited JSON for easier parsing
12+ controller.enqueue(encoder.encode(`${JSON.stringify(event)}\n`));
13+ });
14+
15+ controller.close();
16+ },
17+ });
18+ return new NextResponse(stream, {
19+ headers: {
20+ "Content-Type": "text/event-stream",
21+ "Cache-Control": "no-cache",
22+ Connection: "keep-alive",
23+ },
24+ });
25+}
src/app/layout.tsx+3−3
@@ -28,10 +28,10 @@ export default function RootLayout({
2828 >
2929 <SidebarProvider>
3030 <AppSidebar />
31- <main>
31+ <div className="flex flex-1 flex-col">
3232 <SidebarTrigger />
33- {children}
34- </main>
33+ <main className="flex flex-1 flex-col">{children}</main>
34+ </div>
3535 </SidebarProvider>
3636 </ThemeProvider>
3737 </body>
src/app/page.tsx+3−1
@@ -1,3 +1,5 @@
1+import { Chat } from "@/components/chat";
2+
13 export default function Page() {
2- return <div>{/* Page content */}</div>;
4+ return <Chat />;
35 }
src/components/chat.tsx+416−0
@@ -0,0 +1,416 @@
1+"use client";
2+
3+import {
4+ Bot,
5+ ChevronRight,
6+ Loader2,
7+ Search,
8+ SendHorizontal,
9+ User,
10+ Wrench,
11+} from "lucide-react";
12+import { useCallback, useEffect, useRef, useState } from "react";
13+import { Avatar, AvatarFallback } from "@/components/ui/avatar";
14+import { Button } from "@/components/ui/button";
15+import { Card, CardContent } from "@/components/ui/card";
16+import {
17+ Collapsible,
18+ CollapsibleContent,
19+ CollapsibleTrigger,
20+} from "@/components/ui/collapsible";
21+import { ScrollArea } from "@/components/ui/scroll-area";
22+import { Textarea } from "@/components/ui/textarea";
23+import { cn } from "@/lib/utils";
24+
25+interface ToolCall {
26+ id: string;
27+ name: string;
28+ args: Record<string, unknown>;
29+ status: "pending" | "complete";
30+ result?: string;
31+}
32+
33+interface Message {
34+ id: string;
35+ role: "user" | "assistant";
36+ content: string;
37+ reasoning?: string;
38+ toolCalls?: ToolCall[];
39+}
40+
41+export function Chat() {
42+ const [messages, setMessages] = useState<Message[]>([]);
43+ const [input, setInput] = useState("");
44+ const [isStreaming, setIsStreaming] = useState(false);
45+ const messagesEndRef = useRef<HTMLDivElement>(null);
46+ const textareaRef = useRef<HTMLTextAreaElement>(null);
47+
48+ const scrollToBottom = useCallback(() => {
49+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
50+ }, []);
51+
52+ useEffect(() => {
53+ scrollToBottom();
54+ }, [messages, scrollToBottom]);
55+
56+ const handleSubmit = useCallback(
57+ async (e: React.FormEvent) => {
58+ e.preventDefault();
59+ const trimmedInput = input.trim();
60+ if (!trimmedInput || isStreaming) return;
61+
62+ const userMessage: Message = {
63+ id: crypto.randomUUID(),
64+ role: "user",
65+ content: trimmedInput,
66+ };
67+
68+ const assistantMessageId = crypto.randomUUID();
69+
70+ setInput("");
71+ setMessages((prev) => [...prev, userMessage]);
72+ setIsStreaming(true);
73+ textareaRef.current?.focus();
74+
75+ let assistantContent = "";
76+ let assistantReasoning = "";
77+ const toolCallsMap = new Map<string, ToolCall>();
78+
79+ const updateAssistantMessage = () => {
80+ setMessages((prev) => {
81+ const existing = prev.find((m) => m.id === assistantMessageId);
82+ const newMessage: Message = {
83+ id: assistantMessageId,
84+ role: "assistant",
85+ content: assistantContent,
86+ reasoning: assistantReasoning || undefined,
87+ toolCalls:
88+ toolCallsMap.size > 0
89+ ? Array.from(toolCallsMap.values())
90+ : undefined,
91+ };
92+
93+ if (existing) {
94+ return prev.map((m) =>
95+ m.id === assistantMessageId ? newMessage : m,
96+ );
97+ }
98+ return [...prev, newMessage];
99+ });
100+ };
101+
102+ try {
103+ const response = await fetch("/api/stream", {
104+ method: "POST",
105+ headers: { "Content-Type": "application/json" },
106+ body: JSON.stringify({ userInput: trimmedInput }),
107+ });
108+
109+ if (!response.ok) {
110+ throw new Error("Failed to get response");
111+ }
112+
113+ const reader = response.body?.getReader();
114+ if (!reader) throw new Error("No reader available");
115+
116+ const decoder = new TextDecoder();
117+ let buffer = "";
118+
119+ while (true) {
120+ const { done, value } = await reader.read();
121+ if (done) break;
122+
123+ buffer += decoder.decode(value, { stream: true });
124+
125+ // Parse newline-delimited JSON events
126+ const lines = buffer.split("\n");
127+ buffer = lines.pop() ?? ""; // Keep incomplete line in buffer
128+
129+ for (const line of lines) {
130+ if (!line.trim()) continue;
131+
132+ try {
133+ const event = JSON.parse(line);
134+
135+ switch (event.type) {
136+ case "token":
137+ // Accumulate content tokens
138+ assistantContent += event.content;
139+ break;
140+ case "reasoning":
141+ // Accumulate reasoning tokens
142+ assistantReasoning += event.content;
143+ break;
144+ case "tool_call":
145+ toolCallsMap.set(event.id, {
146+ id: event.id,
147+ name: event.name,
148+ args: event.args,
149+ status: "pending",
150+ });
151+ break;
152+ case "tool_result": {
153+ const existing = toolCallsMap.get(event.id);
154+ if (existing) {
155+ toolCallsMap.set(event.id, {
156+ ...existing,
157+ status: "complete",
158+ result: event.result,
159+ });
160+ }
161+ break;
162+ }
163+ }
164+ } catch {
165+ // Ignore parse errors for partial chunks
166+ }
167+ }
168+
169+ // Update UI after processing each chunk for real-time feedback
170+ if (assistantContent || assistantReasoning || toolCallsMap.size > 0) {
171+ updateAssistantMessage();
172+ }
173+ }
174+
175+ // Finalize: ensure assistant message exists even if empty
176+ setMessages((prev) => {
177+ const hasAssistant = prev.some((m) => m.id === assistantMessageId);
178+ if (!hasAssistant) {
179+ return [
180+ ...prev,
181+ {
182+ id: assistantMessageId,
183+ role: "assistant",
184+ content: "I apologize, I could not respond.",
185+ },
186+ ];
187+ }
188+ return prev;
189+ });
190+ } catch (error) {
191+ console.error("Stream error:", error);
192+ setMessages((prev) => [
193+ ...prev,
194+ {
195+ id: assistantMessageId,
196+ role: "assistant",
197+ content:
198+ "Sorry, there was an error processing your request. Please try again.",
199+ },
200+ ]);
201+ } finally {
202+ setIsStreaming(false);
203+ }
204+ },
205+ [input, isStreaming],
206+ );
207+
208+ const handleKeyDown = useCallback(
209+ (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
210+ if (e.key === "Enter" && !e.shiftKey) {
211+ e.preventDefault();
212+ handleSubmit(e);
213+ }
214+ },
215+ [handleSubmit],
216+ );
217+
218+ return (
219+ <div className="flex flex-1 flex-col">
220+ {/* Messages area */}
221+ <ScrollArea className="flex-1 p-4">
222+ <div className="mx-auto max-w-3xl space-y-4">
223+ {messages.length === 0 ? (
224+ <div className="flex h-[60vh] items-center justify-center">
225+ <div className="text-center">
226+ <Bot className="text-muted-foreground mx-auto size-12" />
227+ <h2 className="mt-4 text-xl font-semibold">
228+ How can I help you today?
229+ </h2>
230+ <p className="text-muted-foreground mt-2">
231+ Ask me anything and I'll search the web for answers.
232+ </p>
233+ </div>
234+ </div>
235+ ) : (
236+ messages.map((message) => (
237+ <MessageBubble key={message.id} message={message} />
238+ ))
239+ )}
240+
241+ {isStreaming && !messages.some((m) => m.role === "assistant") && (
242+ <div className="flex gap-3">
243+ <Avatar>
244+ <AvatarFallback>
245+ <Bot className="size-4" />
246+ </AvatarFallback>
247+ </Avatar>
248+ <Card size="sm" className="max-w-[80%]">
249+ <CardContent>
250+ <div className="flex items-center gap-2">
251+ <Loader2 className="size-4 animate-spin" />
252+ <span className="text-muted-foreground text-sm">
253+ Thinking...
254+ </span>
255+ </div>
256+ </CardContent>
257+ </Card>
258+ </div>
259+ )}
260+
261+ <div ref={messagesEndRef} />
262+ </div>
263+ </ScrollArea>
264+
265+ {/* Input area */}
266+ <div className="border-border bg-background/95 supports-backdrop-filter:bg-background/60 border-t p-4 backdrop-blur">
267+ <form onSubmit={handleSubmit} className="mx-auto max-w-3xl">
268+ <div className="bg-muted/50 relative flex items-end gap-2 rounded-2xl p-2">
269+ <Textarea
270+ ref={textareaRef}
271+ value={input}
272+ onChange={(e) => setInput(e.target.value)}
273+ onKeyDown={handleKeyDown}
274+ placeholder="Ask me anything..."
275+ className="min-h-[44px] flex-1 resize-none border-0 bg-transparent focus-visible:ring-0"
276+ disabled={isStreaming}
277+ />
278+ <Button
279+ type="submit"
280+ size="icon"
281+ disabled={!input.trim() || isStreaming}
282+ className="shrink-0"
283+ >
284+ {isStreaming ? (
285+ <Loader2 className="size-4 animate-spin" />
286+ ) : (
287+ <SendHorizontal className="size-4" />
288+ )}
289+ </Button>
290+ </div>
291+ <p className="text-muted-foreground mt-2 text-center text-xs">
292+ Press Enter to send, Shift+Enter for new line
293+ </p>
294+ </form>
295+ </div>
296+ </div>
297+ );
298+}
299+
300+function MessageBubble({ message }: { message: Message }) {
301+ const isUser = message.role === "user";
302+
303+ return (
304+ <div className={cn("flex gap-3", isUser && "flex-row-reverse")}>
305+ <Avatar className={cn(isUser && "bg-primary text-primary-foreground")}>
306+ <AvatarFallback
307+ className={cn(isUser && "bg-primary text-primary-foreground")}
308+ >
309+ {isUser ? <User className="size-4" /> : <Bot className="size-4" />}
310+ </AvatarFallback>
311+ </Avatar>
312+
313+ {isUser ? (
314+ <Card
315+ size="sm"
316+ className="bg-primary text-primary-foreground max-w-[80%]"
317+ >
318+ <CardContent>
319+ <p className="whitespace-pre-wrap">{message.content}</p>
320+ </CardContent>
321+ </Card>
322+ ) : (
323+ <Card size="sm" className="max-w-[80%]">
324+ {/* Reasoning section */}
325+ {message.reasoning && (
326+ <CardContent>
327+ <Collapsible defaultOpen>
328+ <div className="border-muted-foreground/20 bg-muted/30 rounded-lg border p-2">
329+ <CollapsibleTrigger className="text-muted-foreground hover:text-foreground flex w-full items-center gap-1 text-xs transition-colors [&[data-state=open]>svg:first-child]:rotate-90">
330+ <ChevronRight className="size-3 transition-transform" />
331+ <span>Thinking...</span>
332+ </CollapsibleTrigger>
333+ <CollapsibleContent>
334+ <p className="text-muted-foreground mt-2 whitespace-pre-wrap text-xs">
335+ {message.reasoning}
336+ </p>
337+ </CollapsibleContent>
338+ </div>
339+ </Collapsible>
340+ </CardContent>
341+ )}
342+
343+ {/* Tool calls section */}
344+ {message.toolCalls && message.toolCalls.length > 0 && (
345+ <CardContent className="space-y-1">
346+ {message.toolCalls.map((tc) => (
347+ <ToolCallBubble key={tc.id} toolCall={tc} />
348+ ))}
349+ </CardContent>
350+ )}
351+
352+ {/* Main content */}
353+ {message.content && (
354+ <CardContent>
355+ <p className="whitespace-pre-wrap">{message.content}</p>
356+ </CardContent>
357+ )}
358+ </Card>
359+ )}
360+ </div>
361+ );
362+}
363+
364+function ToolCallBubble({ toolCall }: { toolCall: ToolCall }) {
365+ const isSearch = toolCall.name === "search_web";
366+
367+ return (
368+ <Collapsible>
369+ <Card size="sm" className="border-blue-500/20 bg-blue-500/5">
370+ <CardContent>
371+ <div className="flex items-center gap-2">
372+ {toolCall.status === "pending" ? (
373+ <Loader2 className="size-3 animate-spin text-blue-500" />
374+ ) : isSearch ? (
375+ <Search className="size-3 text-blue-500" />
376+ ) : (
377+ <Wrench className="size-3 text-blue-500" />
378+ )}
379+ <span className="text-xs font-medium">
380+ {isSearch ? "Searching" : toolCall.name}
381+ {isSearch &&
382+ typeof toolCall.args.query === "string" &&
383+ toolCall.args.query && (
384+ <span className="text-muted-foreground font-normal">
385+ {" "}
386+ for "{toolCall.args.query}"
387+ </span>
388+ )}
389+ </span>
390+ {toolCall.status === "complete" && (
391+ <span className="text-muted-foreground text-xs">✓</span>
392+ )}
393+ </div>
394+
395+ {toolCall.result && (
396+ <>
397+ <CollapsibleTrigger className="text-muted-foreground hover:text-foreground mt-1 flex items-center gap-1 text-xs transition-colors [&[data-state=open]>svg:first-child]:rotate-90">
398+ <ChevronRight className="size-3 transition-transform" />
399+ <span>View results</span>
400+ </CollapsibleTrigger>
401+ <CollapsibleContent>
402+ <ScrollArea className="mt-2 max-h-48">
403+ <pre className="bg-muted/50 rounded p-2 text-xs">
404+ {toolCall.result.length > 2000
405+ ? `${toolCall.result.slice(0, 2000)}...`
406+ : toolCall.result}
407+ </pre>
408+ </ScrollArea>
409+ </CollapsibleContent>
410+ </>
411+ )}
412+ </CardContent>
413+ </Card>
414+ </Collapsible>
415+ );
416+}
src/components/ui/avatar.tsx+109−0
@@ -0,0 +1,109 @@
1+"use client"
2+
3+import * as React from "react"
4+import { Avatar as AvatarPrimitive } from "radix-ui"
5+
6+import { cn } from "@/lib/utils"
7+
8+function Avatar({
9+ className,
10+ size = "default",
11+ ...props
12+}: React.ComponentProps<typeof AvatarPrimitive.Root> & {
13+ size?: "default" | "sm" | "lg"
14+}) {
15+ return (
16+ <AvatarPrimitive.Root
17+ data-slot="avatar"
18+ data-size={size}
19+ className={cn(
20+ "size-8 rounded-full after:rounded-full data-[size=lg]:size-10 data-[size=sm]:size-6 after:border-border group/avatar relative flex shrink-0 select-none after:absolute after:inset-0 after:border after:mix-blend-darken dark:after:mix-blend-lighten",
21+ className
22+ )}
23+ {...props}
24+ />
25+ )
26+}
27+
28+function AvatarImage({
29+ className,
30+ ...props
31+}: React.ComponentProps<typeof AvatarPrimitive.Image>) {
32+ return (
33+ <AvatarPrimitive.Image
34+ data-slot="avatar-image"
35+ className={cn(
36+ "rounded-full aspect-square size-full object-cover",
37+ className
38+ )}
39+ {...props}
40+ />
41+ )
42+}
43+
44+function AvatarFallback({
45+ className,
46+ ...props
47+}: React.ComponentProps<typeof AvatarPrimitive.Fallback>) {
48+ return (
49+ <AvatarPrimitive.Fallback
50+ data-slot="avatar-fallback"
51+ className={cn(
52+ "bg-muted text-muted-foreground rounded-full flex size-full items-center justify-center text-sm group-data-[size=sm]/avatar:text-xs",
53+ className
54+ )}
55+ {...props}
56+ />
57+ )
58+}
59+
60+function AvatarBadge({ className, ...props }: React.ComponentProps<"span">) {
61+ return (
62+ <span
63+ data-slot="avatar-badge"
64+ className={cn(
65+ "bg-primary text-primary-foreground ring-background absolute right-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full bg-blend-color ring-2 select-none",
66+ "group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",
67+ "group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",
68+ "group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",
69+ className
70+ )}
71+ {...props}
72+ />
73+ )
74+}
75+
76+function AvatarGroup({ className, ...props }: React.ComponentProps<"div">) {
77+ return (
78+ <div
79+ data-slot="avatar-group"
80+ className={cn(
81+ "*:data-[slot=avatar]:ring-background group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2",
82+ className
83+ )}
84+ {...props}
85+ />
86+ )
87+}
88+
89+function AvatarGroupCount({
90+ className,
91+ ...props
92+}: React.ComponentProps<"div">) {
93+ return (
94+ <div
95+ data-slot="avatar-group-count"
96+ className={cn("bg-muted text-muted-foreground size-8 rounded-full text-sm group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3 ring-background relative flex shrink-0 items-center justify-center ring-2", className)}
97+ {...props}
98+ />
99+ )
100+}
101+
102+export {
103+ Avatar,
104+ AvatarImage,
105+ AvatarFallback,
106+ AvatarGroup,
107+ AvatarGroupCount,
108+ AvatarBadge,
109+}
src/components/ui/card.tsx+14−20
@@ -1,6 +1,6 @@
1-import type * as React from "react";
1+import * as React from "react"
22
3-import { cn } from "@/lib/utils";
3+import { cn } from "@/lib/utils"
44
55 function Card({
66 className,
@@ -11,13 +11,10 @@ function Card({
1111 <div
1212 data-slot="card"
1313 data-size={size}
14- className={cn(
15- "ring-foreground/10 bg-card text-card-foreground gap-6 overflow-hidden rounded-2xl py-6 text-sm ring-1 has-[>img:first-child]:pt-0 data-[size=sm]:gap-4 data-[size=sm]:py-4 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl group/card flex flex-col",
16- className,
17- )}
14+ className={cn("ring-foreground/10 bg-card text-card-foreground gap-6 overflow-hidden rounded-2xl py-6 text-sm ring-1 has-[>img:first-child]:pt-0 data-[size=sm]:gap-4 data-[size=sm]:py-4 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl group/card flex flex-col", className)}
1815 {...props}
1916 />
20- );
17+ )
2118 }
2219
2320 function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
@@ -26,11 +23,11 @@ function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
2623 data-slot="card-header"
2724 className={cn(
2825 "gap-2 rounded-t-xl px-6 group-data-[size=sm]/card:px-4 [.border-b]:pb-6 group-data-[size=sm]/card:[.border-b]:pb-4 group/card-header @container/card-header grid auto-rows-min items-start has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto]",
29- className,
26+ className
3027 )}
3128 {...props}
3229 />
33- );
30+ )
3431 }
3532
3633 function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
@@ -40,7 +37,7 @@ function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
4037 className={cn("text-base font-medium", className)}
4138 {...props}
4239 />
43- );
40+ )
4441 }
4542
4643 function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
@@ -50,7 +47,7 @@ function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
5047 className={cn("text-muted-foreground text-sm", className)}
5148 {...props}
5249 />
53- );
50+ )
5451 }
5552
5653 function CardAction({ className, ...props }: React.ComponentProps<"div">) {
@@ -59,11 +56,11 @@ function CardAction({ className, ...props }: React.ComponentProps<"div">) {
5956 data-slot="card-action"
6057 className={cn(
6158 "col-start-2 row-span-2 row-start-1 self-start justify-self-end",
62- className,
59+ className
6360 )}
6461 {...props}
6562 />
66- );
63+ )
6764 }
6865
6966 function CardContent({ className, ...props }: React.ComponentProps<"div">) {
@@ -73,20 +70,17 @@ function CardContent({ className, ...props }: React.ComponentProps<"div">) {
7370 className={cn("px-6 group-data-[size=sm]/card:px-4", className)}
7471 {...props}
7572 />
76- );
73+ )
7774 }
7875
7976 function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
8077 return (
8178 <div
8279 data-slot="card-footer"
83- className={cn(
84- "rounded-b-xl px-6 group-data-[size=sm]/card:px-4 [.border-t]:pt-6 group-data-[size=sm]/card:[.border-t]:pt-4 flex items-center",
85- className,
86- )}
80+ className={cn("rounded-b-xl px-6 group-data-[size=sm]/card:px-4 [.border-t]:pt-6 group-data-[size=sm]/card:[.border-t]:pt-4 flex items-center", className)}
8781 {...props}
8882 />
89- );
83+ )
9084 }
9185
9286 export {
@@ -97,4 +91,4 @@ export {
9791 CardAction,
9892 CardDescription,
9993 CardContent,
100-};
94+}
src/components/ui/collapsible.tsx+33−0
@@ -0,0 +1,33 @@
1+"use client"
2+
3+import { Collapsible as CollapsiblePrimitive } from "radix-ui"
4+
5+function Collapsible({
6+ ...props
7+}: React.ComponentProps<typeof CollapsiblePrimitive.Root>) {
8+ return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} />
9+}
10+
11+function CollapsibleTrigger({
12+ ...props
13+}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleTrigger>) {
14+ return (
15+ <CollapsiblePrimitive.CollapsibleTrigger
16+ data-slot="collapsible-trigger"
17+ {...props}
18+ />
19+ )
20+}
21+
22+function CollapsibleContent({
23+ ...props
24+}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleContent>) {
25+ return (
26+ <CollapsiblePrimitive.CollapsibleContent
27+ data-slot="collapsible-content"
28+ {...props}
29+ />
30+ )
31+}
32+
33+export { Collapsible, CollapsibleTrigger, CollapsibleContent }
src/components/ui/scroll-area.tsx+55−0
@@ -0,0 +1,55 @@
1+"use client"
2+
3+import * as React from "react"
4+import { ScrollArea as ScrollAreaPrimitive } from "radix-ui"
5+
6+import { cn } from "@/lib/utils"
7+
8+function ScrollArea({
9+ className,
10+ children,
11+ ...props
12+}: React.ComponentProps<typeof ScrollAreaPrimitive.Root>) {
13+ return (
14+ <ScrollAreaPrimitive.Root
15+ data-slot="scroll-area"
16+ className={cn("relative", className)}
17+ {...props}
18+ >
19+ <ScrollAreaPrimitive.Viewport
20+ data-slot="scroll-area-viewport"
21+ className="focus-visible:ring-ring/50 size-full rounded-[inherit] transition-[color,box-shadow] outline-none focus-visible:ring-[3px] focus-visible:outline-1"
22+ >
23+ {children}
24+ </ScrollAreaPrimitive.Viewport>
25+ <ScrollBar />
26+ <ScrollAreaPrimitive.Corner />
27+ </ScrollAreaPrimitive.Root>
28+ )
29+}
30+
31+function ScrollBar({
32+ className,
33+ orientation = "vertical",
34+ ...props
35+}: React.ComponentProps<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>) {
36+ return (
37+ <ScrollAreaPrimitive.ScrollAreaScrollbar
38+ data-slot="scroll-area-scrollbar"
39+ data-orientation={orientation}
40+ orientation={orientation}
41+ className={cn(
42+ "data-horizontal:h-2.5 data-horizontal:flex-col data-horizontal:border-t data-horizontal:border-t-transparent data-vertical:h-full data-vertical:w-2.5 data-vertical:border-l data-vertical:border-l-transparent flex touch-none p-px transition-colors select-none",
43+ className
44+ )}
45+ {...props}
46+ >
47+ <ScrollAreaPrimitive.ScrollAreaThumb
48+ data-slot="scroll-area-thumb"
49+ className="rounded-full bg-border relative flex-1"
50+ />
51+ </ScrollAreaPrimitive.ScrollAreaScrollbar>
52+ )
53+}
54+
55+export { ScrollArea, ScrollBar }
src/lib/chat.ts+108−11
@@ -1,4 +1,10 @@
1+import {
2+ AIMessage,
3+ AIMessageChunk,
4+ ToolMessage,
5+} from "@langchain/core/messages";
16 import { tool } from "@langchain/core/tools";
7+import { concat } from "@langchain/core/utils/stream";
28 import { ChatOllama } from "@langchain/ollama";
39 import { createAgent, SystemMessage } from "langchain";
410 import { z } from "zod";
@@ -16,7 +22,8 @@ const searchWeb = tool(
1622 `http://localhost:8080/search?q=${query}&format=json`,
1723 );
1824 const data = await response.json();
19- return data.results;
25+ // TODO: create a plain text version of the results for the llm
26+ return JSON.stringify(data.results ?? [], null, 2);
2027 },
2128 {
2229 name: "search_web",
@@ -27,7 +34,21 @@ const searchWeb = tool(
2734 },
2835 );
2936
30-export async function chat() {
37+export type StreamEvent =
38+ | { type: "token"; content: string }
39+ | { type: "reasoning"; content: string }
40+ | {
41+ type: "tool_call";
42+ id: string;
43+ name: string;
44+ args: Record<string, unknown>;
45+ }
46+ | { type: "tool_result"; id: string; result: string };
47+
48+export async function chat(
49+ userInput: string,
50+ onEvent: (event: StreamEvent) => void,
51+) {
3152 const llm = new ChatOllama({
3253 model: "qwen3:8b",
3354 });
@@ -36,24 +57,100 @@ export async function chat() {
3657 model: llm,
3758 tools: [searchWeb],
3859 systemPrompt: new SystemMessage(
39- "You have access to a tool that seaches the web." +
40- "Use the tool to help answer user queries.",
60+ "You area helpful web search assistant." +
61+ "You have access to a tool that searches the web." +
62+ "Create optimized search queries from the user's input, use them with the search_web tool to get the best results." +
63+ "Review the results and provide a concise summary of the information found. Including sources and links if available." +
64+ "Use plain text for the response to the user, without any markdown formatting.",
4165 ),
4266 });
4367
44- for await (const chunk of await agent.stream(
68+ // Stream both modes: "messages" for tokens, "updates" for tool calls/results
69+ const stream = await agent.stream(
4570 {
4671 messages: [
4772 {
4873 role: "user",
49- content: "help me search for popular typescript runtimes",
74+ content: userInput,
5075 },
5176 ],
5277 },
53- { streamMode: "updates" },
54- )) {
55- const [step, content] = Object.entries(chunk)[0];
56- console.log(`step: ${step}`);
57- console.log(`content: ${JSON.stringify(content, null, 2)}`);
78+ { streamMode: ["messages", "updates"] },
79+ );
80+
81+ // Track emitted tool calls to avoid duplicates
82+ const emittedToolCallIds = new Set<string>();
83+ let aggregatedAiChunk: AIMessageChunk | undefined;
84+
85+ for await (const [mode, chunk] of stream) {
86+ // Handle token-by-token streaming from "messages" mode
87+ if (mode === "messages") {
88+ const [message, _metadata] = chunk as [unknown, unknown];
89+
90+ if (message instanceof AIMessageChunk) {
91+ // Stream content tokens immediately
92+ if (typeof message.content === "string" && message.content) {
93+ onEvent({ type: "token", content: message.content });
94+ }
95+
96+ // Stream reasoning tokens immediately
97+ const reasoning = message.additional_kwargs?.reasoning_content;
98+ if (typeof reasoning === "string" && reasoning) {
99+ onEvent({ type: "reasoning", content: reasoning });
100+ }
101+
102+ // Aggregate for tool call detection within messages mode
103+ aggregatedAiChunk =
104+ aggregatedAiChunk === undefined
105+ ? message
106+ : concat(aggregatedAiChunk, message);
107+ }
108+ }
109+
110+ // Handle state updates from "updates" mode (tool calls and results)
111+ if (mode === "updates") {
112+ const update = chunk as Record<string, { messages?: unknown[] }>;
113+
114+ for (const [_node, state] of Object.entries(update)) {
115+ if (!state.messages) continue;
116+
117+ for (const msg of state.messages) {
118+ // Handle AI messages with tool calls
119+ if (AIMessage.isInstance(msg)) {
120+ if (msg.tool_calls) {
121+ for (const tc of msg.tool_calls) {
122+ if (tc.id && tc.name && !emittedToolCallIds.has(tc.id)) {
123+ emittedToolCallIds.add(tc.id);
124+ onEvent({
125+ type: "tool_call",
126+ id: tc.id,
127+ name: tc.name,
128+ args: tc.args ?? {},
129+ });
130+ }
131+ }
132+ }
133+
134+ // Handle reasoning from updates
135+ const reasoning = msg.additional_kwargs?.reasoning_content;
136+ if (typeof reasoning === "string" && reasoning) {
137+ onEvent({ type: "reasoning", content: reasoning });
138+ }
139+ }
140+
141+ // Handle tool results
142+ if (ToolMessage.isInstance(msg)) {
143+ onEvent({
144+ type: "tool_result",
145+ id: msg.tool_call_id,
146+ result:
147+ typeof msg.content === "string"
148+ ? msg.content
149+ : JSON.stringify(msg.content),
150+ });
151+ }
152+ }
153+ }
154+ }
58155 }
59156 }