RendyUI English

区块 / Answer with sources

Answer with sources

AI 对话 · React / TypeScript Free

录制演示

Answer with sources 实现预览

源码

chat-answer-sources.tsx
/* Answer with sources — free block published by UI Stacks (public page source).
   Kept verbatim: the author publishes this block free, in full, at https://uistacks.dev/blocks/chat-answer-sources
   See replica/README.md for provenance. */
"use client";

import { useId, useState, type ReactNode } from "react";
import { Message } from "../components/message";
import { Citation } from "../components/citation";
import { CitationRow, type CitationSource } from "../components/citation-row";
import { Feedback } from "../components/feedback";
import { Suggestions } from "../components/suggestions";
import { Avatar } from "../components/avatar";
import { FileText } from "lucide-react";

/* ── Sample data: replace with your own ─────────────────────── */

const CHAT = {
  title: "Refunds over $200 this week",
  assistant: "Claude",
  time: "11:20 AM",
  question: "Which refunds over $200 went out this week, and why?",
  answer:
    "Three refunds over $200 went out between Nov 3 and Nov 9, $956.40 in all. Two were double charges from the Nov 4 card reader outage at Pike Place ($286.40 and $258.00), and one was a cancelled catering order at Capitol Hill ($412.00).",
};

const USER = { name: "Olivia Bennett", src: "/avatars/olivia-bennett.png" };

type Cited = { label: string; title: string; meta: string; quote: string; brand: Brand };
/** The three facts the answer rests on, as numbered citations. Wrap the quoted part in [[double brackets]]. */
const CITED: Cited[] = [
  { label: "Stripe · Refunds", title: "Refunds, Nov 3 – 9", meta: "Stripe · UI Stacks", quote: "3 refunds over $200, [[$956.40 in total]]", brand: "stripe" },
  { label: "Shopify · Orders", title: "Orders #4410 – #4436", meta: "uistacks.myshopify.com", quote: "Charged twice: [[card reader timeout, Pike Place]]", brand: "shopify" },
  { label: "#uistacks-ops", title: "Jamal in #uistacks-ops", meta: "Slack · Nov 4, 8:12 AM", quote: "[[The Pike Place reader is double charging]], swapping it now", brand: "slack" },
];

/** Everything the assistant read before it answered. */
const READ: { id: string; title: string; domain: string; brand?: Brand; cited?: number }[] = [
  { id: "refunds", title: "Refunds, Nov 3 – 9", domain: "dashboard.stripe.com", brand: "stripe", cited: 1 },
  { id: "orders", title: "Orders #4410 – #4436", domain: "uistacks.myshopify.com", brand: "shopify", cited: 2 },
  { id: "ops", title: "#uistacks-ops, Nov 4", domain: "uistacks.slack.com", brand: "slack", cited: 3 },
  { id: "policy", title: "Refund policy v3", domain: "Uploaded Nov 2" },
  { id: "catering", title: "Catering order #4398", domain: "uistacks.myshopify.com", brand: "shopify" },
  { id: "payouts", title: "Payouts, November", domain: "dashboard.stripe.com", brand: "stripe" },
];

/** Follow-ups and the answer each one gets. */
const FOLLOW_UPS: { label: string; answer: string }[] = [
  { label: "Who approved them?", answer: "Marcus Reed approved all three in Stacks Books, each within an hour of the request. None of them waited past the one-day limit in the refund policy." },
  { label: "Is the card reader fixed?", answer: "Yes. Jamal Carter swapped the reader at Pike Place on Nov 5 and there have been no double charges since. I will flag it if one shows up again." },
  { label: "Draft a note to the customers", answer: "Here is a short draft: “Sorry about the double charge on Nov 4. We refunded it in full, and your next drink is on us.” Say the word and I will send it from support@uistacks.dev." },
];

/* ── Brand marks (trademarks belong to their owners) ────────── */

type Brand = "claude" | "stripe" | "shopify" | "slack";

function ClaudeMark({ size = 20 }: { size?: number }) {
  return (
    <svg width={size} height={size} viewBox="-3 -3 30 30" fill="#d97757" aria-hidden>
      <path d="m4.7144 15.9555 4.7174-2.6471.079-.2307-.079-.1275h-.2307l-.7893-.0486-2.6956-.0729-2.3375-.0971-2.2646-.1214-.5707-.1215-.5343-.7042.0546-.3522.4797-.3218.686.0608 1.5179.1032 2.2767.1578 1.6514.0972 2.4468.255h.3886l.0546-.1579-.1336-.0971-.1032-.0972L6.973 9.8356l-2.55-1.6879-1.3356-.9714-.7225-.4918-.3643-.4614-.1578-1.0078.6557-.7225.8803.0607.2246.0607.8925.686 1.9064 1.4754 2.4893 1.8336.3643.3035.1457-.1032.0182-.0728-.164-.2733-1.3539-2.4467-1.445-2.4893-.6435-1.032-.17-.6194c-.0607-.255-.1032-.4674-.1032-.7285L6.287.1335 6.6997 0l.9957.1336.419.3642.6192 1.4147 1.0018 2.2282 1.5543 3.0296.4553.8985.2429.8318.091.255h.1579v-.1457l.1275-1.706.2368-2.0947.2307-2.6957.0789-.7589.3764-.9107.7468-.4918.5828.2793.4797.686-.0668.4433-.2853 1.8517-.5586 2.9021-.3643 1.9429h.2125l.2429-.2429.9835-1.3053 1.6514-2.0643.7286-.8196.85-.9046.5464-.4311h1.0321l.759 1.1293-.34 1.1657-1.0625 1.3478-.8804 1.1414-1.2628 1.7-.7893 1.36.0729.1093.1882-.0183 2.8535-.607 1.5421-.2794 1.8396-.3157.8318.3886.091.3946-.3278.8075-1.967.4857-2.3072.4614-3.4364.8136-.0425.0304.0486.0607 1.5482.1457.6618.0364h1.621l3.0175.2247.7892.522.4736.6376-.079.4857-1.2142.6193-1.6393-.3886-3.825-.9107-1.3113-.3279h-.1822v.1093l1.0929 1.0686 2.0035 1.8092 2.5075 2.3314.1275.5768-.3218.4554-.34-.0486-2.2039-1.6575-.85-.7468-1.9246-1.621h-.1275v.17l.4432.6496 2.3436 3.5214.1214 1.0807-.17.3521-.6071.2125-.6679-.1214-1.3721-1.9246L14.38 17.959l-1.1414-1.9428-.1397.079-.674 7.2552-.3156.3703-.7286.2793-.6071-.4614-.3218-.7468.3218-1.4753.3886-1.9246.3157-1.53.2853-1.9004.17-.6314-.0121-.0425-.1397.0182-1.4328 1.9672-2.1796 2.9446-1.7243 1.8456-.4128.164-.7164-.3704.0667-.6618.4008-.5889 2.386-3.0357 1.4389-1.882.929-1.0868-.0062-.1579h-.0546l-6.3385 4.1164-1.1293.1457-.4857-.4554.0608-.7467.2307-.2429 1.9064-1.3114Z" />
    </svg>
  );
}

function StripeMark() {
  return (
    <svg viewBox="0 0 24 24" fill="#635bff" aria-hidden className="size-full">
      <path d="M13.976 9.15c-2.172-.806-3.356-1.426-3.356-2.409 0-.831.683-1.305 1.901-1.305 2.227 0 4.515.858 6.09 1.631l.89-5.494C18.252.975 15.697 0 12.165 0 9.667 0 7.589.654 6.104 1.872 4.56 3.147 3.757 4.992 3.757 7.218c0 4.039 2.467 5.76 6.476 7.219 2.585.92 3.445 1.574 3.445 2.583 0 .98-.84 1.545-2.354 1.545-1.875 0-4.965-.921-6.99-2.109l-.9 5.555C5.175 22.99 8.385 24 11.714 24c2.641 0 4.843-.624 6.328-1.813 1.664-1.305 2.525-3.236 2.525-5.732 0-4.128-2.524-5.851-6.594-7.305h.003z" />
    </svg>
  );
}

function ShopifyMark() {
  return (
    <svg viewBox="0 0 256 292" aria-hidden className="size-full">
      <path d="M223.774 57.34c-.201-1.46-1.48-2.268-2.537-2.357-1.055-.088-23.383-1.743-23.383-1.743s-15.507-15.395-17.209-17.099c-1.703-1.703-5.029-1.185-6.32-.805-.19.056-3.388 1.043-8.678 2.68-5.18-14.906-14.322-28.604-30.405-28.604-.444 0-.901.018-1.358.044C129.31 3.407 123.644.779 118.75.779c-37.465 0-55.364 46.835-60.976 70.635-14.558 4.511-24.9 7.718-26.221 8.133-8.126 2.549-8.383 2.805-9.45 10.462C21.3 95.806.038 260.235.038 260.235l165.678 31.042 89.77-19.42S223.973 58.8 223.775 57.34zM156.49 40.848l-14.019 4.339c.005-.988.01-1.96.01-3.023 0-9.264-1.286-16.723-3.349-22.636 8.287 1.04 13.806 10.469 17.358 21.32zm-27.638-19.483c2.304 5.773 3.802 14.058 3.802 25.238 0 .572-.005 1.095-.01 1.624-9.117 2.824-19.024 5.89-28.953 8.966 5.575-21.516 16.025-31.908 25.161-35.828zm-11.131-10.537c1.617 0 3.246.549 4.805 1.622-12.007 5.65-24.877 19.88-30.312 48.297l-22.886 7.088C75.694 46.16 90.81 10.828 117.72 10.828z" fill="#95BF46" />
      <path d="M221.237 54.983c-1.055-.088-23.383-1.743-23.383-1.743s-15.507-15.395-17.209-17.099c-.637-.634-1.496-.959-2.394-1.099l-12.527 256.233 89.762-19.418S223.972 58.8 223.774 57.34c-.201-1.46-1.48-2.268-2.537-2.357" fill="#5E8E3E" />
      <path d="M135.242 104.585l-11.069 32.926s-9.698-5.176-21.586-5.176c-17.428 0-18.305 10.937-18.305 13.693 0 15.038 39.2 20.8 39.2 56.024 0 27.713-17.577 45.558-41.277 45.558-28.44 0-42.984-17.7-42.984-17.7l7.615-25.16s14.95 12.835 27.565 12.835c8.243 0 11.596-6.49 11.596-11.232 0-19.616-32.16-20.491-32.16-52.724 0-27.129 19.472-53.382 58.778-53.382 15.145 0 22.627 4.338 22.627 4.338" fill="#FFF" />
    </svg>
  );
}

function SlackMark() {
  return (
    <svg viewBox="0 0 2447.6 2452.5" fillRule="evenodd" clipRule="evenodd" aria-hidden className="size-full">
      <path d="m897.4 0c-135.3.1-244.8 109.9-244.7 245.2-.1 135.3 109.5 245.1 244.8 245.2h244.8v-245.1c.1-135.3-109.5-245.1-244.9-245.3.1 0 .1 0 0 0m0 654h-652.6c-135.3.1-244.9 109.9-244.8 245.2-.2 135.3 109.4 245.1 244.7 245.3h652.7c135.3-.1 244.9-109.9 244.8-245.2.1-135.4-109.5-245.2-244.8-245.3z" fill="#36c5f0" />
      <path d="m2447.6 899.2c.1-135.3-109.5-245.1-244.8-245.2-135.3.1-244.9 109.9-244.8 245.2v245.3h244.8c135.3-.1 244.9-109.9 244.8-245.3zm-652.7 0v-654c.1-135.2-109.4-245-244.7-245.2-135.3.1-244.9 109.9-244.8 245.2v654c-.2 135.3 109.4 245.1 244.7 245.3 135.3-.1 244.9-109.9 244.8-245.3z" fill="#2eb67d" />
      <path d="m1550.1 2452.5c135.3-.1 244.9-109.9 244.8-245.2.1-135.3-109.5-245.1-244.8-245.2h-244.8v245.2c-.1 135.2 109.5 245 244.8 245.2zm0-654.1h652.7c135.3-.1 244.9-109.9 244.8-245.2.2-135.3-109.4-245.1-244.7-245.3h-652.7c-135.3.1-244.9 109.9-244.8 245.2-.1 135.4 109.4 245.2 244.7 245.3z" fill="#ecb22e" />
      <path d="m0 1553.2c-.1 135.3 109.5 245.1 244.8 245.2 135.3-.1 244.9-109.9 244.8-245.2v-245.2h-244.8c-135.3.1-244.9 109.9-244.8 245.2zm652.7 0v654c-.2 135.3 109.4 245.1 244.7 245.3 135.3-.1 244.9-109.9 244.8-245.2v-653.9c.2-135.3-109.4-245.1-244.7-245.3-135.4 0-244.9 109.8-244.8 245.1 0 0 0 .1 0 0" fill="#e01e5a" />
    </svg>
  );
}

const MARKS: Record<Exclude<Brand, "claude">, () => ReactNode> = { stripe: StripeMark, shopify: ShopifyMark, slack: SlackMark };
/** A brand mark in a 16 px rounded tile, the way the kit shows app logos in chips. */
const tile = (brand?: Brand) => {
  if (!brand || brand === "claude") return <FileText size={16} strokeWidth={1.5} className="text-fg-muted" aria-hidden />;
  const M = MARKS[brand];
  return (
    <span className="block size-4 overflow-hidden rounded-[4px]">
      <M />
    </span>
  );
};

/* ── The block ──────────────────────────────────────────────── */

type Turn = { id: number; question: string; answer: string };

/** An assistant answer with numbered citations, the sources it read, feedback and follow-ups that get answered. */
export default function ChatAnswerSourcesBlock({ className = "" }: { className?: string }) {
  const [turns, setTurns] = useState<Turn[]>([]);
  const [retries, setRetries] = useState(0);
  const [status, setStatus] = useState("");
  const asked = new Set(turns.map((t) => t.question));
  const left = FOLLOW_UPS.filter((f) => !asked.has(f.label));
  const sources: CitationSource[] = READ.map((s) => ({ id: s.id, title: s.title, domain: s.domain, icon: tile(s.brand) }));
  const me = <Avatar name={USER.name} src={USER.src} size="xs" />;
  const titleId = useId();

  const ask = (label: string) => {
    const f = FOLLOW_UPS.find((x) => x.label === label);
    if (!f) return;
    setTurns((t) => [...t, { id: t.length, question: f.label, answer: f.answer }]);
    setStatus(`${CHAT.assistant} answered: ${f.label}`);
  };

  return (
    <section aria-labelledby={titleId} className={"@container w-full min-w-0 " + className}>
      <div className="rounded-[20px] bg-surface p-4 shadow-[var(--shadow-card)] @min-[480px]:p-6">
        <header className="flex items-center gap-3 border-b border-border pb-4">
          <span className="grid size-10 shrink-0 place-items-center rounded-full bg-subtle">
            <ClaudeMark />
          </span>
          <div className="min-w-0 flex-1">
            <h2 id={titleId} className="truncate text-[15px] font-medium leading-5 text-fg">
              {CHAT.title}
            </h2>
            <p className="mt-0.5 truncate text-[13px] leading-5 text-fg-muted">
              {CHAT.assistant} · Today, {CHAT.time}
            </p>
          </div>
        </header>

        <div className="grid grid-cols-1 gap-8 pt-5 @min-[1024px]:grid-cols-[minmax(0,1fr)_280px]">
          <div className="flex min-w-0 flex-col gap-6">
            <Message role="user" avatar={me} content={CHAT.question} />

            <div className="flex flex-col gap-4">
              <Message key={retries} role="assistant" name={CHAT.assistant} time={CHAT.time} avatar={<ClaudeMark />} content={CHAT.answer} streaming={retries > 0} />
              <div className="flex flex-wrap items-center gap-2">
                {CITED.map((c, i) => (
                  <Citation key={c.label} index={i + 1} label={c.label} title={c.title} meta={c.meta} quote={c.quote} icon={tile(c.brand)} />
                ))}
              </div>
              {/* Narrow: the sources fold into one chip. Wide: they get their own column. */}
              <div className="@min-[1024px]:hidden">
                <CitationRow sources={sources} label={`Read ${sources.length} sources`} previewCount={3} />
              </div>
              <Feedback
                text={CHAT.answer}
                onRetry={() => {
                  setRetries((r) => r + 1);
                  setStatus(`${CHAT.assistant} wrote the answer again`);
                }}
                onFeedback={(v) => setStatus(v === "up" ? "Thanks, marked as helpful" : "Thanks, your note goes to the team")}
                className="-ml-1.5"
              />
            </div>

            {turns.map((t) => (
              <div key={t.id} className="flex flex-col gap-6">
                <Message role="user" avatar={me} content={t.question} />
                <Message role="assistant" name={CHAT.assistant} time="Now" avatar={<ClaudeMark />} content={t.answer} streaming />
              </div>
            ))}

            {left.length > 0 ? (
              <div className="flex flex-col gap-3 border-t border-border pt-4">
                <p className="text-[13px] leading-5 text-fg-muted">Ask a follow-up</p>
                <Suggestions items={left.map((f) => ({ label: f.label }))} onSelect={ask} />
              </div>
            ) : (
              <p className="border-t border-border pt-4 text-[13px] leading-5 text-fg-muted">That covers this answer. Start a new chat for anything else.</p>
            )}
          </div>

          <aside aria-labelledby={titleId + "-src"} className="hidden self-start rounded-[16px] bg-subtle p-4 @min-[1024px]:block">
            <h3 id={titleId + "-src"} className="text-[13px] font-medium leading-5 text-fg">
              Sources <span className="text-fg-muted">· {READ.length}</span>
            </h3>
            <p className="mt-0.5 text-[12px] leading-4 text-fg-muted">The numbered ones are cited in the answer</p>
            <ol className="mt-3 flex flex-col gap-1">
              {READ.map((r) => (
                <li key={r.id} className="flex items-center gap-3 py-1.5">
                  <span className="grid size-8 shrink-0 place-items-center rounded-[8px] bg-surface shadow-[inset_0_0_0_1px_var(--border)]">{tile(r.brand)}</span>
                  <span className="min-w-0 flex-1">
                    <span className="block truncate text-[13px] font-medium leading-5 text-fg">{r.title}</span>
                    <span className="block truncate text-[12px] leading-4 text-fg-muted">{r.domain}</span>
                  </span>
                  {r.cited && (
                    <span className="grid size-5 shrink-0 place-items-center rounded-full bg-surface text-[11px] font-medium tabular-nums text-fg shadow-[inset_0_0_0_1px_var(--border)]">
                      <span className="sr-only">Cited as </span>
                      {r.cited}
                    </span>
                  )}
                </li>
              ))}
            </ol>
          </aside>
        </div>
      </div>
      <span role="status" aria-live="polite" className="sr-only">
        {status}
      </span>
    </section>
  );
}

使用说明

把 TSX 加入 React 项目,并安装组件引用的依赖。页面样式依赖 Tailwind CSS 与 主题变量。

包依赖 · lucide-react、react

共享源码 · avatar.tsx、citation.tsx、citation-row.tsx、feedback.tsx、message.tsx、suggestions.tsx