{
  "name": "ai-chat",
  "type": "registry:block",
  "framework": "react",
  "title": "AI chat",
  "description": "A conversation with an assistant: starter prompts, markdown replies, a typing state and the composer, composed from the registry.",
  "registryDependencies": [
    "@newtui/chat",
    "@newtui/message-list",
    "@newtui/message-group",
    "@newtui/message-composer",
    "@newtui/markdown",
    "@newtui/typing-indicator",
    "@newtui/avatar",
    "@newtui/button"
  ],
  "files": [
    {
      "path": "blocks/ai-chat/ai-chat.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { Avatar } from \"@/registry/bases/newt/ui/avatar\"\nimport { Button } from \"@/registry/bases/newt/ui/button\"\nimport { Chat, ChatBody, ChatFooter } from \"@/registry/bases/newt/ui/chat\"\nimport { Markdown } from \"@/registry/bases/newt/ui/markdown\"\nimport { MessageComposer } from \"@/registry/bases/newt/ui/message-composer\"\nimport {\n  MessageGroup,\n  MessageGroupAuthor,\n  MessageGroupAvatar,\n  MessageGroupBody,\n  MessageGroupBotTag,\n  MessageGroupContent,\n  MessageGroupHeader,\n  MessageGroupTime,\n} from \"@/registry/bases/newt/ui/message-group\"\nimport { MessageList } from \"@/registry/bases/newt/ui/message-list\"\nimport { TypingIndicator } from \"@/registry/bases/newt/ui/typing-indicator\"\n\n/*\n * A conversation with an assistant: who you are talking to, the exchange so\n * far, and the composer. Before the first message the list gives way to a\n * greeting and a few prompts to start from; while a reply is on its way the\n * assistant is shown typing.\n *\n * The block knows nothing about a model or a transport. It draws `messages`\n * and reports what was sent; calling the model, streaming the reply into the\n * array and setting `pending` are the application's.\n */\nexport interface AiChatMessage {\n  id: string\n  role: \"user\" | \"assistant\"\n  /** An assistant's body is rendered as chat markdown; a user's as written. */\n  body: string\n  sentAt: string\n  /** Rendered timestamp; the ISO value goes in `sentAt`. */\n  displayTime: string\n}\n\nexport interface AiChatProps extends React.ComponentProps<\"div\"> {\n  assistantName: string\n  /** Said quietly beside the name: the model, or what the assistant is for. */\n  caption?: string\n  userName?: string\n  messages: readonly AiChatMessage[]\n  /** A reply is being written: the assistant is shown typing. */\n  pending?: boolean\n  /** The line above the prompts, before the first message. */\n  greeting?: string\n  /** Prompts offered before the first message; choosing one sends it. */\n  suggestions?: readonly string[]\n  onSend?: (message: string) => void\n  /**\n   * Called when the composer's add button is pressed. The composer always\n   * draws that button, so without this it does nothing: open your own picker.\n   */\n  onAttach?: () => void\n}\n\nfunction initial(name: string): string {\n  return name.trim().charAt(0).toUpperCase()\n}\n\nexport function AiChat({\n  assistantName,\n  caption,\n  userName = \"You\",\n  messages,\n  pending = false,\n  greeting = \"How can I help?\",\n  suggestions = [],\n  onSend,\n  onAttach,\n  className,\n  ...props\n}: AiChatProps) {\n  const [value, setValue] = React.useState(\"\")\n  const empty = messages.length === 0 && !pending\n\n  /*\n   * New text is followed while the reader is at the bottom and left alone once\n   * they have scrolled up to read something older. Sending always follows.\n   */\n  const body = React.useRef<HTMLDivElement>(null)\n  const following = React.useRef(true)\n  React.useEffect(() => {\n    const element = body.current\n    if (following.current && element) element.scrollTop = element.scrollHeight\n  }, [messages, pending])\n\n  return (\n    <div\n      className={cn(\n        \"overflow-hidden rounded-[var(--newt-radius-lg)] border border-newt-border bg-newt-bg-elevated\",\n        className\n      )}\n      {...props}\n    >\n      <header className=\"flex items-center gap-3 border-b border-newt-border px-4 py-3\">\n        <Avatar>{initial(assistantName)}</Avatar>\n        <div className=\"flex min-w-0 flex-col\">\n          <span className=\"flex items-center gap-1.5 font-display text-[15px] font-semibold text-newt-text-primary\">\n            {assistantName}\n            <MessageGroupBotTag aria-label=\"AI assistant\">\n              AI\n            </MessageGroupBotTag>\n          </span>\n          {caption && (\n            <span className=\"truncate text-xs text-newt-text-muted\">\n              {caption}\n            </span>\n          )}\n        </div>\n      </header>\n\n      <Chat aria-label={`Conversation with ${assistantName}`} className=\"h-96\">\n        <ChatBody\n          ref={body}\n          focusable\n          viewportLabel={`Messages with ${assistantName}`}\n          onScroll={(event) => {\n            const { scrollHeight, scrollTop, clientHeight } =\n              event.currentTarget\n            following.current = scrollHeight - scrollTop - clientHeight < 48\n          }}\n        >\n          {empty ? (\n            <div className=\"flex flex-1 flex-col items-center justify-center gap-4 px-4 py-8 text-center\">\n              <p className=\"font-display text-xl font-semibold text-newt-text-primary\">\n                {greeting}\n              </p>\n              {suggestions.length > 0 && (\n                <div className=\"flex max-w-md flex-wrap justify-center gap-2\">\n                  {suggestions.map((suggestion) => (\n                    <Button\n                      key={suggestion}\n                      variant=\"secondary\"\n                      size=\"sm\"\n                      className=\"border border-newt-border\"\n                      onClick={() => onSend?.(suggestion)}\n                    >\n                      {suggestion}\n                    </Button>\n                  ))}\n                </div>\n              )}\n            </div>\n          ) : (\n            <MessageList label={`Messages with ${assistantName}`}>\n              {messages.map((message) => {\n                const fromAssistant = message.role === \"assistant\"\n                const author = fromAssistant ? assistantName : userName\n                return (\n                  <MessageGroup key={message.id}>\n                    <MessageGroupAvatar>\n                      <Avatar>{initial(author)}</Avatar>\n                    </MessageGroupAvatar>\n                    <MessageGroupBody>\n                      <MessageGroupHeader>\n                        <MessageGroupAuthor>{author}</MessageGroupAuthor>\n                        {fromAssistant && (\n                          <MessageGroupBotTag aria-label=\"AI assistant\">\n                            AI\n                          </MessageGroupBotTag>\n                        )}\n                        <MessageGroupTime dateTime={message.sentAt}>\n                          {message.displayTime}\n                        </MessageGroupTime>\n                      </MessageGroupHeader>\n                      <MessageGroupContent>\n                        {fromAssistant ? (\n                          <Markdown content={message.body} />\n                        ) : (\n                          <span className=\"whitespace-pre-wrap\">\n                            {message.body}\n                          </span>\n                        )}\n                      </MessageGroupContent>\n                    </MessageGroupBody>\n                  </MessageGroup>\n                )\n              })}\n            </MessageList>\n          )}\n        </ChatBody>\n        <ChatFooter>\n          {/*\n           * The status region is always there, so that its text appearing is\n           * what a screen reader announces; mounted together with its text, a\n           * live region is often missed.\n           */}\n          <div role=\"status\" className=\"flex h-6 items-center px-1\">\n            {pending && (\n              <TypingIndicator role=\"none\" aria-label={undefined}>\n                {assistantName} is writing…\n              </TypingIndicator>\n            )}\n          </div>\n          <MessageComposer\n            channelName={assistantName}\n            aria-label={`Message ${assistantName}`}\n            onAdd={onAttach}\n            placeholder={`Ask ${assistantName} anything`}\n            value={value}\n            onValueChange={setValue}\n            onSubmit={(message) => {\n              following.current = true\n              onSend?.(message)\n              setValue(\"\")\n            }}\n          />\n        </ChatFooter>\n      </Chat>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ]
}