Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

README.md

121 lines4.6 KB

1# AG-UI Polyglot Chat
2​
3A React SPA that talks to **Go, Rust, PHP, Zig, Bash, and Python AG-UI backends** over Server-Sent Events (SSE). Each backend streams simple chat completions from **OpenAI** or **Anthropic** — no TanStack packages on the server, just the AG-UI wire protocol.
4​
5This example shows that any backend can serve `@tanstack/ai-react` clients as long as it speaks [AG-UI](https://docs.ag-ui.com/): accept `RunAgentInput` via POST, stream AG-UI events as SSE.
6​
7## Tech stack
8​
9| Layer | Stack |
10| ------------- | ------------------------------------------------------------------------ |
11| Client | React, Vite, `@tanstack/ai-react`, `@tanstack/ai-react/ui` |
12| Go server | `net/http`, hand-rolled AG-UI SSE, OpenAI/Anthropic streaming on `:8001` |
13| Rust server | Axum, hand-rolled AG-UI SSE, OpenAI/Anthropic streaming on `:8002` |
14| PHP server | Built-in PHP server + curl, hand-rolled AG-UI SSE on `:8003` |
15| Zig server | Stdlib HTTP + HTTPS client, hand-rolled AG-UI SSE on `:8004` |
16| Bash server | Bash + socat + curl + jq, hand-rolled AG-UI SSE on `:8005` |
17| Python server | Stdlib HTTP + urllib, hand-rolled AG-UI SSE on `:8006` |
18​
19## Prerequisites
20​
21- Node.js + pnpm (from repo root)
22- [Go 1.22+](https://go.dev/dl/) (optional — UI shows setup if missing)
23- [Rust stable](https://rustup.rs/) (optional)
24- [PHP 8.2+ CLI with curl](https://www.php.net/downloads) (optional)
25- [Zig](https://ziglang.org/download/) (optional)
26- Bash 4+, curl, jq, and socat (`brew install bash jq socat`) (optional)
27- [Python 3.9+](https://www.python.org/downloads/) (optional)
28- OpenAI and/or Anthropic API keys
29​
30Only backends whose toolchains are detected on PATH are started by `pnpm dev:all`. The UI reads `public/servers.json` to show which servers are available and displays setup instructions for missing ones.
31​
32## Quick start
33​
34From the monorepo root:
35​
36```bash
37pnpm install
38cd examples/ag-ui
39cp .env.example .env
40# Add OPENAI_API_KEY and/or ANTHROPIC_API_KEY
41pnpm dev:all
42```
43​
44Open [http://localhost:3000](http://localhost:3000), pick a backend tab, choose a provider/model, and chat.
45​
46### Simulate missing toolchains
47​
48If your machine has every runtime installed but you want to test the setup UI:
49​
50```bash
51AGUI_DISABLE_SERVERS=php,zig pnpm dev:all
52```
53​
54PHP and Zig tabs will show install instructions; Go and Rust still connect normally.
55​
56### Run pieces separately
57​
58```bash
59# Terminal 1 — Vite dev server (proxies /api/* and serves servers.json)
60pnpm dev
61​
62# Terminal 2+ — individual backends
63pnpm dev:go
64pnpm dev:rust
65pnpm dev:php
66pnpm dev:zig
67pnpm dev:bash
68pnpm dev:python
69```
70​
71`pnpm dev:all` loads `.env`, writes `public/servers.json`, and starts the client plus every detected backend.
72​
73Refresh availability without restarting everything:
74​
75```bash
76pnpm detect-servers
77```
78​
79## Architecture
80​
81```
82React SPA (useChat + fetchServerSentEvents)
83 GET /servers.json ──► generated backend availability
84 POST /api/go ──► Vite proxy ──► Go :8001 ──► OpenAI / Anthropic
85 POST /api/rust ──► Vite proxy ──► Rust :8002 ──► OpenAI / Anthropic
86 POST /api/php ──► Vite proxy ──► PHP :8003 ──► OpenAI / Anthropic
87 POST /api/zig ──► Vite proxy ──► Zig :8004 ──► OpenAI / Anthropic
88 POST /api/bash ──► Vite proxy ──► Bash :8005 ──► OpenAI / Anthropic
89 POST /api/python ──► Vite proxy ──► Python :8006 ──► OpenAI / Anthropic
90```
91​
92The client sends AG-UI `RunAgentInput` with `forwardedProps: { provider, model }`. Each server converts simple text messages, streams the provider response, and emits:
93​
94```
95RUN_STARTED
96TEXT_MESSAGE_START
97TEXT_MESSAGE_CONTENT (streamed)
98TEXT_MESSAGE_END
99RUN_FINISHED
100```
101​
102## Project layout
103​
104```
105examples/ag-ui/
106├── public/servers.json generated backend availability (fallback committed)
107├── scripts/ detect toolchains, write servers.json, dev:all
108├── src/ React SPA
109├── servers/go/ Go AG-UI + LLM server
110├── servers/rust/ Rust AG-UI + LLM server
111├── servers/php/ PHP AG-UI + LLM server
112├── servers/zig/ Zig AG-UI + LLM server
113├── servers/bash/ Bash AG-UI + LLM server
114└── servers/python/ Python AG-UI + LLM server
115```
116​
117## Related docs
118​
119- [AG-UI compliance migration](../../docs/migration/ag-ui-compliance.md)
120- [Transports](../../docs/transports/overview.md)
121- [Chat architecture](../../packages/ai/docs/chat-architecture.md)