Knowledge base
Retrieval knowledge-base card tracking document indexing status with retry controls and a Test retrieval tab showing ranked chunks.
pnpm dlx shadcn@latest add @haze/knowledge-base---
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>When to use
- Managing RAG source documents across parsing, embedding and failed states with retry
- Testing semantic chunk retrieval and cosine similarity scores before deploying an agent
When not to use
- Showing a simple file uploader without indexing or retrieval testing; use file-upload
Installation
Add the Haze registry to components.json once, then install Knowledge base with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/knowledge-basenpx shadcn@latest add @haze/knowledge-baseyarn dlx shadcn@latest add @haze/knowledge-basebunx --bun shadcn@latest add @haze/knowledge-baseFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-knowledge-base" | Unique prefix for form labels and heading IDs. |
| heading | string | "Knowledge base" | Card heading. |
| docs | { id: string; name: string; chunks: number; state: string }[] | 4 indexed documents | Documents with chunk counts and pipeline states. |
| class | string | – | Extra classes on the outer section. |
Keyboard interactions
- Tab
- Move across view tabs, Add document, Retry buttons and retrieval query form
- EnterSpace
- Switch tabs, retry a failed document or run a test retrieval query
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.