|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useChat } from "@ai-sdk/react"; |
| 4 | +import { DefaultChatTransport } from "ai"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | +export default function Chat() { |
| 8 | + const [input, setInput] = useState(""); |
| 9 | + const [errorMessage, setErrorMessage] = useState<string | null>(null); |
| 10 | + |
| 11 | + const { messages, sendMessage, status } = useChat({ |
| 12 | + transport: new DefaultChatTransport({ api: "/chat/test" }), |
| 13 | + onError: async (e) => { |
| 14 | + setErrorMessage(e.message); |
| 15 | + }, |
| 16 | + }); |
| 17 | + |
| 18 | + return ( |
| 19 | + <main className="page"> |
| 20 | + <style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style> |
| 21 | + <div className="section"> |
| 22 | + <h1 className="heading-primary">AI chat</h1> |
| 23 | + <p className="typography-primary"> |
| 24 | + This chat is protected by Arcjet: bot detection blocks automated |
| 25 | + clients, a token bucket rate limits AI usage, sensitive information |
| 26 | + detection prevents data leaks, and prompt injection detection stops |
| 27 | + manipulation attempts. |
| 28 | + </p> |
| 29 | + </div> |
| 30 | + |
| 31 | + <hr className="divider" /> |
| 32 | + |
| 33 | + <div className="section"> |
| 34 | + <h2 className="heading-secondary">Try it</h2> |
| 35 | + |
| 36 | + {messages.length > 0 && ( |
| 37 | + <div |
| 38 | + style={{ |
| 39 | + display: "flex", |
| 40 | + flexDirection: "column", |
| 41 | + gap: "0.75rem", |
| 42 | + width: "100%", |
| 43 | + }} |
| 44 | + > |
| 45 | + {messages.map((message) => ( |
| 46 | + <div |
| 47 | + key={message.id} |
| 48 | + style={{ |
| 49 | + display: "flex", |
| 50 | + flexDirection: "column", |
| 51 | + gap: "0.25rem", |
| 52 | + alignSelf: |
| 53 | + message.role === "user" ? "flex-end" : "flex-start", |
| 54 | + maxWidth: "80%", |
| 55 | + }} |
| 56 | + > |
| 57 | + <span |
| 58 | + style={{ |
| 59 | + fontSize: "0.75rem", |
| 60 | + fontWeight: 600, |
| 61 | + color: "var(--theme-text-muted)", |
| 62 | + textTransform: "uppercase", |
| 63 | + letterSpacing: "0.05em", |
| 64 | + alignSelf: |
| 65 | + message.role === "user" ? "flex-end" : "flex-start", |
| 66 | + }} |
| 67 | + > |
| 68 | + {message.role === "user" ? "You" : "AI"} |
| 69 | + </span> |
| 70 | + <div |
| 71 | + style={{ |
| 72 | + padding: "0.75rem 1rem", |
| 73 | + borderRadius: "0.5rem", |
| 74 | + border: "1px solid var(--theme-border-level1)", |
| 75 | + backgroundColor: |
| 76 | + message.role === "user" |
| 77 | + ? "var(--theme-foreground)" |
| 78 | + : "var(--theme-surface)", |
| 79 | + color: |
| 80 | + message.role === "user" |
| 81 | + ? "var(--theme-background)" |
| 82 | + : "var(--theme-text-primary)", |
| 83 | + fontSize: "0.9375rem", |
| 84 | + lineHeight: "1.5rem", |
| 85 | + whiteSpace: "pre-wrap", |
| 86 | + }} |
| 87 | + > |
| 88 | + {message.parts.map((part, i) => { |
| 89 | + switch (part.type) { |
| 90 | + case "text": |
| 91 | + return ( |
| 92 | + <span key={`${message.id}-${i}`}>{part.text}</span> |
| 93 | + ); |
| 94 | + } |
| 95 | + })} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ))} |
| 99 | + </div> |
| 100 | + )} |
| 101 | + |
| 102 | + {status === "submitted" && ( |
| 103 | + <div |
| 104 | + style={{ |
| 105 | + display: "flex", |
| 106 | + alignItems: "center", |
| 107 | + gap: "0.5rem", |
| 108 | + fontSize: "0.875rem", |
| 109 | + color: "var(--theme-text-muted)", |
| 110 | + }} |
| 111 | + > |
| 112 | + <span |
| 113 | + style={{ |
| 114 | + display: "inline-block", |
| 115 | + width: "1rem", |
| 116 | + height: "1rem", |
| 117 | + border: "2px solid var(--theme-border-level1)", |
| 118 | + borderTopColor: "var(--theme-text-muted)", |
| 119 | + borderRadius: "50%", |
| 120 | + animation: "spin 0.8s linear infinite", |
| 121 | + }} |
| 122 | + /> |
| 123 | + AI is thinking… |
| 124 | + </div> |
| 125 | + )} |
| 126 | + |
| 127 | + {errorMessage && ( |
| 128 | + <div |
| 129 | + style={{ |
| 130 | + fontSize: "0.875rem", |
| 131 | + fontWeight: 600, |
| 132 | + lineHeight: "1.25rem", |
| 133 | + padding: "0.75rem", |
| 134 | + borderRadius: "0.5rem", |
| 135 | + border: "1px solid #ef4444", |
| 136 | + backgroundColor: "#2d0a0a", |
| 137 | + color: "#fca5a5", |
| 138 | + }} |
| 139 | + > |
| 140 | + {errorMessage} |
| 141 | + </div> |
| 142 | + )} |
| 143 | + |
| 144 | + <form |
| 145 | + onSubmit={(e) => { |
| 146 | + e.preventDefault(); |
| 147 | + setErrorMessage(null); |
| 148 | + sendMessage({ text: input }); |
| 149 | + setInput(""); |
| 150 | + }} |
| 151 | + className="form form--wide" |
| 152 | + > |
| 153 | + <div className="form-field"> |
| 154 | + <label className="form-label"> |
| 155 | + Message |
| 156 | + <input |
| 157 | + className="form-input" |
| 158 | + value={input} |
| 159 | + placeholder="Say something..." |
| 160 | + onChange={(e) => setInput(e.currentTarget.value)} |
| 161 | + /> |
| 162 | + </label> |
| 163 | + </div> |
| 164 | + <button type="submit" className="button-primary form-button"> |
| 165 | + Send |
| 166 | + </button> |
| 167 | + </form> |
| 168 | + </div> |
| 169 | + </main> |
| 170 | + ); |
| 171 | +} |
0 commit comments