diff --git a/apps/web/app/(app)/[workspaceSlug]/ai/page.tsx b/apps/web/app/(app)/[workspaceSlug]/ai/page.tsx index bdc0759..766fec9 100644 --- a/apps/web/app/(app)/[workspaceSlug]/ai/page.tsx +++ b/apps/web/app/(app)/[workspaceSlug]/ai/page.tsx @@ -1,72 +1,56 @@ "use client"; -import { useState, useRef, useEffect } from "react"; +import { useEffect, useRef } from "react"; import { useParams } from "next/navigation"; -import { Loader2, Send, Sparkles, Bot, User } from "lucide-react"; +import { useChat } from "@ai-sdk/react"; +import { AlertTriangle, Bot, Loader2, Send, Sparkles, User } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; -type Message = { - id: string; - role: "user" | "assistant"; - content: string; -}; - export default function AIPage() { const params = useParams(); - const workspaceSlug = params.workspaceSlug as string; + const workspaceSlug = + typeof params?.workspaceSlug === "string" ? params.workspaceSlug : ""; - const [messages, setMessages] = useState([]); - const [input, setInput] = useState(""); - const [isLoading, setIsLoading] = useState(false); const scrollRef = useRef(null); const inputRef = useRef(null); - // Auto-scroll on new messages + const { + messages, + input, + handleInputChange, + handleSubmit, + status, + error, + setInput, + } = useChat({ + api: "/api/chat", + body: { workspace: workspaceSlug }, + }); + + const isBusy = status === "submitted" || status === "streaming"; + useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } - }, [messages]); - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - const trimmed = input.trim(); - if (!trimmed || isLoading) return; - - const userMsg: Message = { id: crypto.randomUUID(), role: "user", content: trimmed }; - setMessages((prev) => [...prev, userMsg]); - setInput(""); - setIsLoading(true); - - // Placeholder AI response - setTimeout(() => { - const assistantMsg: Message = { - id: crypto.randomUUID(), - role: "assistant", - content: - "I'm your AI assistant. Full AI integration is coming soon! I'll be able to help you create tasks, documents, and manage your workspace.", - }; - setMessages((prev) => [...prev, assistantMsg]); - setIsLoading(false); - }, 1000); - }; + }, [messages, status]); return (
- {/* Header */}

AI Assistant

-

Ask me anything about your workspace

+

+ Ask me anything about your workspace +

- {/* Messages */}
{messages.length === 0 ? (
@@ -76,22 +60,25 @@ export default function AIPage() {

How can I help you today?

- Ask me to create tasks, summarize documents, or manage your workspace. + Ask me to break down a task, draft an outline, or summarize what + you have open.

- {["Create a new project", "Summarize my tasks", "Help me plan a sprint"].map( - (suggestion) => ( - - ), - )} + {[ + "Break this project into subtasks", + "Summarize my open tasks", + "Draft a sprint plan for this week", + ].map((suggestion) => ( + + ))}
) : ( @@ -99,19 +86,28 @@ export default function AIPage() { {messages.map((msg) => (
- {msg.role === "user" ? : } + {msg.role === "user" ? ( + + ) : ( + + )}
))} - {isLoading && ( + {status === "submitted" && (
@@ -133,26 +129,40 @@ export default function AIPage() { )}
)} + + {error ? : null}
- {/* Input */}
-
+