|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + AssistantRuntimeProvider, |
| 5 | + MessagePrimitive, |
| 6 | + ThreadPrimitive, |
| 7 | + useLocalRuntime, |
| 8 | +} from "@assistant-ui/react"; |
| 9 | +import { FeedbackButtons } from "@assistant-ui/chords"; |
| 10 | +import { DemoWrapper } from "./demo-wrapper"; |
| 11 | +import type { ChatModelAdapter } from "@assistant-ui/react"; |
| 12 | +import { FC } from "react"; |
| 13 | + |
| 14 | +const demoAdapter: ChatModelAdapter = { |
| 15 | + async *run({ abortSignal }) { |
| 16 | + const text = |
| 17 | + "Here's a helpful response! Try clicking the thumbs up or thumbs down buttons below to submit feedback."; |
| 18 | + for (let i = 0; i < text.length; i++) { |
| 19 | + if (abortSignal.aborted) return; |
| 20 | + await new Promise((r) => setTimeout(r, 12)); |
| 21 | + yield { |
| 22 | + content: [{ type: "text" as const, text: text.slice(0, i + 1) }], |
| 23 | + }; |
| 24 | + } |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +export function FeedbackButtonsDemo() { |
| 29 | + const runtime = useLocalRuntime(demoAdapter, { |
| 30 | + adapters: { |
| 31 | + feedback: { |
| 32 | + submit: async () => {}, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + return ( |
| 38 | + <DemoWrapper> |
| 39 | + <AssistantRuntimeProvider runtime={runtime}> |
| 40 | + <ThreadPrimitive.Root className="flex h-87.5 flex-col rounded-xl border border-zinc-400 dark:border-zinc-800 bg-white text-zinc-900 dark:bg-zinc-950 dark:text-white px-8"> |
| 41 | + <div className="relative min-h-0 flex-1"> |
| 42 | + <ThreadPrimitive.Viewport className="flex h-full flex-col gap-2 overflow-y-auto px-4 pt-4 pb-4"> |
| 43 | + <ThreadPrimitive.Messages |
| 44 | + components={{ |
| 45 | + UserMessage: DemoUserMessage, |
| 46 | + AssistantMessage: DemoAssistantMessage, |
| 47 | + }} |
| 48 | + /> |
| 49 | + </ThreadPrimitive.Viewport> |
| 50 | + </div> |
| 51 | + <div className="border-t border-zinc-300 dark:border-zinc-800 px-4 py-2"> |
| 52 | + <ThreadPrimitive.If running={false}> |
| 53 | + <ThreadPrimitive.Suggestion |
| 54 | + prompt="Give me a helpful tip" |
| 55 | + send |
| 56 | + className="w-full rounded-lg dark:bg-white/5 bg-black/10 px-3 py-2 text-left text-sm dark:text-white/60 text-black dark:hover:bg-white/10 hover:bg-black/15 transition-colors" |
| 57 | + > |
| 58 | + Send a message to see feedback buttons |
| 59 | + </ThreadPrimitive.Suggestion> |
| 60 | + </ThreadPrimitive.If> |
| 61 | + </div> |
| 62 | + </ThreadPrimitive.Root> |
| 63 | + </AssistantRuntimeProvider> |
| 64 | + </DemoWrapper> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +const DemoUserMessage: FC = () => ( |
| 69 | + <MessagePrimitive.Root className="group/message mx-auto flex w-full max-w-3xl flex-col items-end gap-1"> |
| 70 | + <div className="max-w-[80%] rounded-3xl bg-zinc-100 px-5 text-zinc-900 dark:bg-white/10 dark:text-white/90"> |
| 71 | + <MessagePrimitive.Content /> |
| 72 | + </div> |
| 73 | + </MessagePrimitive.Root> |
| 74 | +); |
| 75 | + |
| 76 | +function DemoAssistantMessage() { |
| 77 | + return ( |
| 78 | + <MessagePrimitive.Root className="group/message mx-auto flex w-full max-w-3xl gap-3"> |
| 79 | + <div className="mt-2.5 flex size-8 shrink-0 items-center justify-center rounded-full border border-zinc-300 text-xs shadow dark:border-white/15"> |
| 80 | + A |
| 81 | + </div> |
| 82 | + <div className="flex-1 pt-0.5"> |
| 83 | + <MessagePrimitive.Parts |
| 84 | + components={{ |
| 85 | + Text: ({ text }) => ( |
| 86 | + <span className="text-sm dark:text-white/90 text-black"> |
| 87 | + {text} |
| 88 | + </span> |
| 89 | + ), |
| 90 | + }} |
| 91 | + /> |
| 92 | + <div className="mt-2"> |
| 93 | + <FeedbackButtons /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </MessagePrimitive.Root> |
| 97 | + ); |
| 98 | +} |
0 commit comments