|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useChat } from "@ai-sdk/react"; |
| 4 | +import { TriggerChatTransport } from "@trigger.dev/sdk/chat"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | +export function Chat({ accessToken }: { accessToken: string }) { |
| 8 | + const [input, setInput] = useState(""); |
| 9 | + |
| 10 | + const { messages, sendMessage, status, error } = useChat({ |
| 11 | + transport: new TriggerChatTransport({ |
| 12 | + task: "ai-chat", |
| 13 | + accessToken, |
| 14 | + baseURL: process.env.NEXT_PUBLIC_TRIGGER_API_URL, |
| 15 | + }), |
| 16 | + }); |
| 17 | + |
| 18 | + function handleSubmit(e: React.FormEvent) { |
| 19 | + e.preventDefault(); |
| 20 | + if (!input.trim() || status === "streaming") return; |
| 21 | + |
| 22 | + sendMessage({ text: input }); |
| 23 | + setInput(""); |
| 24 | + } |
| 25 | + |
| 26 | + return ( |
| 27 | + <div className="flex flex-col rounded-xl border border-gray-200 bg-white shadow-sm"> |
| 28 | + {/* Messages */} |
| 29 | + <div className="flex-1 space-y-4 overflow-y-auto p-4" style={{ maxHeight: "60vh" }}> |
| 30 | + {messages.length === 0 && ( |
| 31 | + <p className="text-center text-sm text-gray-400">Send a message to start chatting.</p> |
| 32 | + )} |
| 33 | + |
| 34 | + {messages.map((message) => ( |
| 35 | + <div |
| 36 | + key={message.id} |
| 37 | + className={`flex ${message.role === "user" ? "justify-end" : "justify-start"}`} |
| 38 | + > |
| 39 | + <div |
| 40 | + className={`max-w-[80%] rounded-lg px-4 py-2 text-sm ${ |
| 41 | + message.role === "user" |
| 42 | + ? "bg-blue-600 text-white" |
| 43 | + : "bg-gray-100 text-gray-900" |
| 44 | + }`} |
| 45 | + > |
| 46 | + {message.parts.map((part, i) => { |
| 47 | + if (part.type === "text") { |
| 48 | + return <span key={i}>{part.text}</span>; |
| 49 | + } |
| 50 | + return null; |
| 51 | + })} |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + ))} |
| 55 | + |
| 56 | + {status === "streaming" && ( |
| 57 | + <div className="flex justify-start"> |
| 58 | + <div className="rounded-lg bg-gray-100 px-4 py-2 text-sm text-gray-400"> |
| 59 | + Thinking… |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + )} |
| 63 | + </div> |
| 64 | + |
| 65 | + {/* Error */} |
| 66 | + {error && ( |
| 67 | + <div className="border-t border-red-100 bg-red-50 px-4 py-2 text-sm text-red-600"> |
| 68 | + {error.message} |
| 69 | + </div> |
| 70 | + )} |
| 71 | + |
| 72 | + {/* Input */} |
| 73 | + <form onSubmit={handleSubmit} className="flex gap-2 border-t border-gray-200 p-4"> |
| 74 | + <input |
| 75 | + type="text" |
| 76 | + value={input} |
| 77 | + onChange={(e) => setInput(e.target.value)} |
| 78 | + placeholder="Type a message…" |
| 79 | + className="flex-1 rounded-lg border border-gray-300 px-3 py-2 text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500" |
| 80 | + /> |
| 81 | + <button |
| 82 | + type="submit" |
| 83 | + disabled={!input.trim() || status === "streaming"} |
| 84 | + className="rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50" |
| 85 | + > |
| 86 | + Send |
| 87 | + </button> |
| 88 | + </form> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments