# Knowledge base

> Retrieval knowledge-base card tracking document indexing status with retry controls and a Test retrieval tab showing ranked chunks.

- Kind: block · Category: Agents · Interactivity: script
- Install: `npx shadcn@latest add @haze/knowledge-base`
- File: `src/components/haze/blocks/knowledge-base.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/blocks/knowledge-base

## Use when
- Managing RAG source documents across parsing, embedding and failed states with retry
- Testing semantic chunk retrieval and cosine similarity scores before deploying an agent

## Avoid when
- Showing a simple file uploader without indexing or retrieval testing; use file-upload

## Alternatives
- [file-upload](https://hazeui.com/ui/file-upload.md)
- [ai-search-bar](https://hazeui.com/blocks/ai-search-bar.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` | "haze-knowledge-base" | no | Unique prefix for form labels and heading IDs. |
| heading | `string` | "Knowledge base" | no | Card heading. |
| docs | `{ id: string; name: string; chunks: number; state: string }[]` | 4 indexed documents | no | Documents with chunk counts and pipeline states. |
| class | `string` |  | no | Extra classes on the outer section. |

## Accessibility
- Documents and Test retrieval views switch via labelled role="tab" buttons.
- Retry and retrieval query updates announce feedback via a polite role="status" element.
- Key Tab: Move across view tabs, Add document, Retry buttons and retrieval query form
- Key Enter / Space: Switch tabs, retry a failed document or run a test retrieval query

## Agent prompts

### Install and use

Install the Haze UI block "Knowledge base" with `npx shadcn@latest add @haze/knowledge-base`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import KnowledgeBase from "@/components/haze/blocks/knowledge-base.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/blocks/knowledge-base.md

### Customize with real content

Adapt the Haze knowledge-base block for our product. Replace the demo values of these props with our real content: id, heading, docs, class. Keep the layout, the heading level rules and these accessibility details: Documents and Test retrieval views switch via labelled role="tab" buttons. Retry and retrieval query updates announce feedback via a polite role="status" element. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using knowledge-base, check it fits: use it when managing rag source documents across parsing, embedding and failed states with retry; testing semantic chunk retrieval and cosine similarity scores before deploying an agent. Avoid it when showing a simple file uploader without indexing or retrieval testing; use file-upload. Alternatives: @haze/file-upload, @haze/ai-search-bar.

## Example requests that should lead here
- Create a RAG knowledge base card with document indexing states, Retry on failure and a Test retrieval tab

## Source

```astro
---
import { BookOpen, FileText, RefreshCw, Search, Upload } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface DocRecord {
  id: string;
  name: string;
  chunks: number;
  state: "Indexed" | "Embedding" | "Failed";
}

interface Props {
  id?: string;
  heading?: string;
  docs?: DocRecord[];
  class?: string;
}

const {
  id = "haze-knowledge-base",
  heading = "Knowledge base",
  docs = [
    { id: "d1", name: "product-architecture.md", chunks: 84, state: "Indexed" },
    { id: "d2", name: "enterprise-sla-2026.pdf", chunks: 42, state: "Indexed" },
    { id: "d3", name: "api-changelog-q4.md", chunks: 19, state: "Embedding" },
    { id: "d4", name: "legacy-pricing-scan.pdf", chunks: 0, state: "Failed" },
  ],
  class: className,
} = Astro.props;
---

<section aria-labelledby={`${id}-title`} data-haze-kb class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
  <div class="mx-auto max-w-3xl rounded-2xl border border-border bg-card p-5 sm:p-6">
    <header class="flex flex-wrap items-center justify-between gap-3 border-b border-border pb-4">
      <div class="flex items-center gap-2.5">
        <span class="grid size-8 place-items-center rounded-lg border border-frame bg-muted text-muted-foreground" aria-hidden="true">
          <BookOpen class="size-4" />
        </span>
        <div>
          <h2 id={`${id}-title`} class="text-base/6 font-semibold tracking-tight text-foreground">{heading}</h2>
          <p class="text-xs/5 text-muted-foreground">145 chunks indexed · text-embedding-3-large</p>
        </div>
      </div>

      <div role="tablist" aria-label="Knowledge base views" class="inline-flex rounded-lg border border-frame bg-muted p-0.5">
        <button type="button" role="tab" data-kb-tab="docs" aria-selected="true" class="inline-flex h-7 cursor-pointer items-center rounded-md px-2.5 text-xs font-medium text-muted-foreground aria-selected:bg-background aria-selected:text-foreground aria-selected:shadow-key focus-visible:outline-2 focus-visible:outline-ring">Documents</button>
        <button type="button" role="tab" data-kb-tab="test" aria-selected="false" class="inline-flex h-7 cursor-pointer items-center rounded-md px-2.5 text-xs font-medium text-muted-foreground aria-selected:bg-background aria-selected:text-foreground aria-selected:shadow-key focus-visible:outline-2 focus-visible:outline-ring">Test retrieval</button>
      </div>
    </header>

    <div data-kb-pane="docs" class="mt-4 space-y-3">
      <div class="flex justify-end">
        <button type="button" data-kb-add class="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-lg bg-primary px-3 text-xs font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
          <Upload class="size-3.5" aria-hidden="true" />
          Add document
        </button>
      </div>
      <ul role="list" class="divide-y divide-border rounded-xl border border-border bg-muted/40">
        {
          docs.map((doc) => (
            <li data-kb-doc class="flex flex-wrap items-center justify-between gap-3 p-3.5">
              <div class="flex min-w-0 items-center gap-2.5">
                <FileText class="size-4 shrink-0 text-muted-foreground" aria-hidden="true" />
                <div class="min-w-0">
                  <p class="truncate text-sm/5 font-medium text-foreground">{doc.name}</p>
                  <p class="font-mono text-[0.6875rem] text-muted-foreground tabular-nums">{doc.chunks} chunks</p>
                </div>
              </div>
              <div class="flex items-center gap-2">
                <span data-kb-state class={cn(
                  "rounded-md border border-frame px-2 py-0.5 text-[0.6875rem]/4 font-medium",
                  doc.state === "Indexed" ? "border-frame-success bg-success/10 text-success" :
                  doc.state === "Embedding" ? "border-frame bg-muted text-foreground" :
                  "border-frame-destructive bg-destructive/10 text-destructive",
                )}>{doc.state}</span>
                {doc.state === "Failed" && (
                  <button type="button" data-kb-retry={doc.name} class="inline-flex h-7 cursor-pointer items-center gap-1 rounded-md border border-frame bg-field px-2 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
                    <RefreshCw class="size-3" aria-hidden="true" />
                    Retry
                  </button>
                )}
              </div>
            </li>
          ))
        }
      </ul>
    </div>

    <div data-kb-pane="test" hidden class="mt-4 space-y-3">
      <form data-kb-form class="flex gap-2">
        <div class="relative flex-1">
          <Search class="pointer-events-none absolute top-1/2 left-3 size-3.5 -translate-y-1/2 text-muted-foreground" aria-hidden="true" />
          <label for={`${id}-query`} class="sr-only">Test retrieval query</label>
          <input id={`${id}-query`} type="text" value="Sev-1 response SLA" class="h-9 w-full rounded-lg border border-frame-input bg-field pr-3 pl-8 text-xs text-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground" />
        </div>
        <button type="submit" class="inline-flex h-9 cursor-pointer items-center rounded-lg bg-primary px-3.5 text-xs font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Query</button>
      </form>

      <ol role="list" class="space-y-2">
        <li class="rounded-xl border border-border bg-muted/40 p-3.5">
          <div class="flex items-center justify-between gap-2 text-xs">
            <span class="font-mono font-medium text-foreground">enterprise-sla-2026.pdf · chunk #11</span>
            <span class="font-mono font-semibold text-foreground tabular-nums">0.94 cosine</span>
          </div>
          <p class="mt-1.5 text-xs/5 text-pretty text-muted-foreground">Sev-1 production outages receive a 1-hour initial response SLA and dedicated Slack Connect escalation.</p>
        </li>
        <li class="rounded-xl border border-border bg-muted/40 p-3.5">
          <div class="flex items-center justify-between gap-2 text-xs">
            <span class="font-mono font-medium text-foreground">product-architecture.md · chunk #48</span>
            <span class="font-mono font-semibold text-foreground tabular-nums">0.81 cosine</span>
          </div>
          <p class="mt-1.5 text-xs/5 text-pretty text-muted-foreground">Multi-region failover completes automatically within 45 seconds for Enterprise clusters.</p>
        </li>
      </ol>
    </div>

    <p data-kb-status role="status" class="mt-3 min-h-4 text-xs/4 text-muted-foreground"></p>
  </div>
</section>

<script>
  function initKnowledgeBases() {
    document.querySelectorAll<HTMLElement>("[data-haze-kb]").forEach((root) => {
      if (root.hasAttribute("data-enhanced")) return;
      root.setAttribute("data-enhanced", "");
      const status = root.querySelector<HTMLElement>("[data-kb-status]");

      root.querySelectorAll<HTMLButtonElement>("[data-kb-tab]").forEach((tab) => {
        tab.addEventListener("click", () => {
          const id = tab.dataset.kbTab;
          root.querySelectorAll<HTMLButtonElement>("[data-kb-tab]").forEach((t) => t.setAttribute("aria-selected", String(t === tab)));
          root.querySelectorAll<HTMLElement>("[data-kb-pane]").forEach((p) => (p.hidden = p.dataset.kbPane !== id));
        });
      });

      root.querySelectorAll<HTMLButtonElement>("[data-kb-retry]").forEach((btn) => {
        btn.addEventListener("click", () => {
          const state = btn.closest("[data-kb-doc]")?.querySelector<HTMLElement>("[data-kb-state]");
          if (state) {
            state.textContent = "Embedding";
            state.className = "rounded-md border border-frame bg-muted px-2 py-0.5 text-[0.6875rem]/4 font-medium text-foreground";
          }
          btn.remove();
          if (status) status.textContent = `Re-queued ${btn.dataset.kbRetry} for parsing and embedding.`;
        });
      });

      root.querySelector<HTMLButtonElement>("[data-kb-add]")?.addEventListener("click", () => {
        if (status) status.textContent = "Opened document picker for knowledge ingestion.";
      });

      root.querySelector<HTMLFormElement>("[data-kb-form]")?.addEventListener("submit", (ev) => {
        ev.preventDefault();
        if (status) status.textContent = "Retrieved top 2 ranked chunks in 42 ms.";
      });
    });
  }
  initKnowledgeBases();
  document.addEventListener("astro:page-load", initKnowledgeBases);
</script>
```
