|
| 1 | +# A2UI PDF Analyst |
| 2 | + |
| 3 | +Chat with your PDF and watch the agent build the UI for each answer. Powered by **A2UI v0.9 (Agent-to-UI)** — the open protocol that lets an agent describe a surface as structured component operations your frontend renders against its own design system. Same chat input, two rendering strategies, one shared 21-component catalog. |
| 4 | + |
| 5 | +https://github.com/user-attachments/assets/c053d2e8-1d40-43cb-8c5a-8e5c121b851f |
| 6 | + |
| 7 | +**Three routes:** |
| 8 | + |
| 9 | +- **`/fixed`** — hand-authored JSON dashboard. The agent only extracts the data (KPIs, trend, segment splits, table rows) and fills the slots. Predictable layout, brand-locked, single LLM call per turn. Best when the shape of the answer is known up front. |
| 10 | +- **`/dynamic`** — no pre-written layout. The agent reads the question, picks components from the catalog, and composes the surface on the fly. A net-income query lands as a single StatCard; a segment breakdown becomes a DonutChart; a research-paper summary composes Overline + Heading + Text + Callout + BulletList. Best when the right answer's _form_ varies with the question. |
| 11 | +- **`/catalog`** — every component rendered live, filterable by group (Layout, Content, Data viz, Interactive). Doubles as a sanity check on the renderers and a reference for what the agent is allowed to draw from. |
| 12 | + |
| 13 | +All three routes share the same brand tokens (`src/a2ui/theme.css`), the same React renderers (`src/a2ui/catalog/renderers.tsx`), and the same client-side PDF text extraction pipeline (`src/lib/pdf.ts`). Re-skin one stylesheet, every surface updates. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- Node.js 20+ and [pnpm](https://pnpm.io/) (npm works too) |
| 18 | +- Python 3.12 |
| 19 | +- [uv](https://docs.astral.sh/uv/) for the Python agent |
| 20 | +- An OpenAI API key |
| 21 | + |
| 22 | +## Run locally |
| 23 | + |
| 24 | +```bash |
| 25 | +git clone https://github.com/CopilotKit/CopilotKit.git |
| 26 | +cd CopilotKit/examples/showcases/a2ui-pdf-analyst |
| 27 | +cp agent/.env.example agent/.env # then put your OPENAI_API_KEY in agent/.env |
| 28 | +pnpm install # installs Next.js + runs `uv sync` for the agent |
| 29 | +pnpm dev # boots web on :3000, agent on :8123 |
| 30 | +``` |
| 31 | + |
| 32 | +Open <http://localhost:3000>. `npm install && npm run dev` works identically. |
| 33 | + |
| 34 | +## Environment variables |
| 35 | + |
| 36 | +`agent/.env`: |
| 37 | + |
| 38 | +| Variable | Required | Notes | |
| 39 | +| ---------------- | -------- | ------------------------------------------------------------------------------------- | |
| 40 | +| `OPENAI_API_KEY` | yes | used by the main agent and by the secondary LLMs inside `query_pdf` / `generate_a2ui` | |
| 41 | + |
| 42 | +## Architecture |
| 43 | + |
| 44 | +``` |
| 45 | +a2ui-pdf-analyst/ |
| 46 | +├── package.json → Next.js manifest + concurrently runs the agent alongside |
| 47 | +├── next.config.ts |
| 48 | +├── postcss.config.mjs |
| 49 | +├── tsconfig.json |
| 50 | +├── public/ → static assets (CopilotKit brand SVGs) |
| 51 | +├── src/ → Next.js 16 · React 19 · Tailwind v4 |
| 52 | +│ ├── app/ |
| 53 | +│ │ ├── api/copilotkit/ → CopilotKit V2 runtime endpoint (HttpAgent → Python) |
| 54 | +│ │ ├── fixed/ → fixed-schema route: pre-authored dashboard |
| 55 | +│ │ ├── dynamic/ → dynamic-schema route: agent invents the layout |
| 56 | +│ │ ├── catalog/ → live showcase of all 21 components |
| 57 | +│ │ ├── globals.css → app-wide tokens, fonts |
| 58 | +│ │ ├── layout.tsx → root layout + Providers |
| 59 | +│ │ └── page.tsx → overview |
| 60 | +│ ├── a2ui/ |
| 61 | +│ │ ├── catalog/ |
| 62 | +│ │ │ ├── definitions.ts → Zod prop schemas + agent-facing descriptions |
| 63 | +│ │ │ ├── renderers.tsx → React renderers (Recharts charts, tables, cards) |
| 64 | +│ │ │ └── index.ts → createCatalog() (definitions + renderers, catalogId) |
| 65 | +│ │ ├── theme.css → brand tokens, scoped to .a2ui-surface |
| 66 | +│ │ ├── surface-bus.ts → per-agent A2UI op stream the canvas subscribes to |
| 67 | +│ │ └── MirrorRenderer.tsx → activity renderer that forwards ops to the canvas |
| 68 | +│ ├── components/ |
| 69 | +│ │ ├── SurfaceCanvas.tsx → mounts A2UIProvider + renders surfaces |
| 70 | +│ │ ├── FilteredUserMessage.tsx → strips inlined PDF text from chat |
| 71 | +│ │ ├── FilteredAssistantMessage.tsx → suppresses JSON-shaped agent replies |
| 72 | +│ │ ├── Split.tsx → VS-Code-style resizable chat/canvas split |
| 73 | +│ │ ├── Providers.tsx → <CopilotKit> + activity renderers |
| 74 | +│ │ └── Brand.tsx → SiteNav + PageHeader |
| 75 | +│ └── lib/pdf.ts → client-side PDF text extraction (pdfjs-dist) |
| 76 | +└── agent/ → Python · LangChain · LangGraph · FastAPI · AG-UI |
| 77 | + ├── main.py → /fixed and /dynamic FastAPI endpoints |
| 78 | + ├── pyproject.toml |
| 79 | + ├── uv.lock |
| 80 | + └── src/ |
| 81 | + ├── catalog.py → CATALOG_ID + system-prompt fragment listing components |
| 82 | + ├── fixed_agent.py → render_dashboard backend tool |
| 83 | + ├── dynamic_agent.py → query_pdf + generate_a2ui tools |
| 84 | + ├── pdf_tools.py → query_pdf: PDF text → structured JSON answer |
| 85 | + ├── multimodal_middleware.py → ag-ui-langgraph patch so PDF text survives the trip to OpenAI |
| 86 | + └── a2ui/schemas/dashboard.json → the fixed dashboard layout (Stack / Grid / charts / table) |
| 87 | +``` |
| 88 | + |
| 89 | +## How it works |
| 90 | + |
| 91 | +**PDF attachment** — CopilotKit's multimodal attachment support lets the user attach a PDF directly in the chat input. The frontend extracts the full text client-side via `pdfjs-dist` and inlines it into the user message under a `[Document: <filename>]` header. `multimodal_middleware.py` patches `ag-ui-langgraph` so this text block survives serialization and arrives intact at OpenAI. The agent scans every message in the conversation history for the most recent `[Document: ...]` header — attach once, ask many questions. |
| 92 | + |
| 93 | +**Fixed schema (`/fixed`)** — `agent/src/a2ui/schemas/dashboard.json` is a static A2UI component tree the agent never touches. The `render_dashboard` tool takes typed arguments (KPIs, trend, share, rows, scope chips), packages them as A2UI `update_data_model` ops, and the existing tree picks them up via `{path}` bindings. One LLM pass, one tool call, surface streams in. |
| 94 | + |
| 95 | +**Dynamic schema (`/dynamic`)** — five steps per turn: |
| 96 | + |
| 97 | +1. User attaches a PDF and asks a question. Frontend inlines the PDF text into the message. |
| 98 | +2. Agent calls `query_pdf` → a sub-LLM reads the document and returns structured JSON: `shape_hint`, `title`, `summary`, `data`. |
| 99 | +3. Agent calls `generate_a2ui` (no arguments) → spawns a second sub-LLM bound to a no-op `render_a2ui` shim with `tool_choice` forced to that shim. |
| 100 | +4. The second LLM's tool-call arguments (surfaceId, catalogId, components, data) become A2UI `create_surface` + `update_components` + `update_data_model` operations. |
| 101 | +5. The JS-side A2UI middleware detects `a2ui_operations` in the tool result and emits the snapshot events the canvas listens for. Surface renders. Agent emits an empty chat message. |
| 102 | + |
| 103 | +## Sample PDFs |
| 104 | + |
| 105 | +These work well for the dynamic-schema demo: |
| 106 | + |
| 107 | +- Apple Q4 FY24 Consolidated Financial Statements ([download](https://www.apple.com/newsroom/pdfs/fy2024-q4/FY24_Q4_Consolidated_Financial_Statements.pdf)) — structured tables, multiple categorical breakdowns |
| 108 | +- Tesla Q3 2024 Update ([download](https://www.tesla.com/sites/default/files/downloads/TSLA-Q3-2024-Update.pdf)) — multi-quarter time-series + production / delivery pairs |
| 109 | +- Anthropic's _Constitutional AI: Harmlessness from AI Feedback_ ([download](https://arxiv.org/pdf/2212.08073)) — research paper, mostly prose, for text-heavy explainer surfaces |
| 110 | + |
| 111 | +## Prompts to try |
| 112 | + |
| 113 | +On `/dynamic` after attaching a PDF: |
| 114 | + |
| 115 | +| Ask the agent | Expected surface | |
| 116 | +| --------------------------------------------------------------------------------------------- | ------------------------------------- | |
| 117 | +| `What was net income last quarter?` | one StatCard | |
| 118 | +| `Break iPhone vs Mac vs iPad vs Wearables vs Services as a donut.` | DonutChart | |
| 119 | +| `Show Q4 net sales by category as horizontal bars.` | HorizontalBarChart | |
| 120 | +| `Plot quarterly production against deliveries across the last 5 quarters as a scatter chart.` | ScatterChart | |
| 121 | +| `Explain the main idea of this paper in plain English.` | Heading + Text + Callout + BulletList | |
| 122 | +| `Show me the revenue trend over the last 6 quarters.` | LineChart | |
| 123 | + |
| 124 | +On `/fixed` after attaching a PDF: |
| 125 | + |
| 126 | +| Ask the agent | What happens | |
| 127 | +| ------------------------------------------- | ---------------------------------------------------------------------- | |
| 128 | +| `Render the dashboard.` | full dashboard with KPIs, trend chart, share donut, table, scope chips | |
| 129 | +| `Switch scope to FY24.` (or click the chip) | re-renders the same dashboard with FY24 data | |
| 130 | + |
| 131 | +## Tech stack |
| 132 | + |
| 133 | +| Layer | Stack | |
| 134 | +| -------------- | -------------------------------------------------------------------------------------------------------------------------------------- | |
| 135 | +| Frontend | Next.js 16 · React 19 · Tailwind v4 · TypeScript · `@copilotkit/react-core/v2` · `@copilotkit/a2ui-renderer` · `pdfjs-dist` · Recharts | |
| 136 | +| Runtime bridge | `@copilotkit/runtime/v2` · `@ag-ui/client` (HttpAgent) | |
| 137 | +| Backend | Python 3.12 · FastAPI · `ag-ui-langgraph` · `copilotkit` (Python SDK) · `langchain` agents + LangGraph · `langchain-openai` | |
| 138 | +| Model | `gpt-5.5` for both the main agent and the secondary LLMs | |
0 commit comments