0
Fork 0
mirror of https://github.com/TanStack/ai.git synced 2026-11-16 19:38:41 +00:00
ai/examples
Tom Beckenham c17bc951ca
feat: add generateWorld() and generateLiveVideo() for prompt-steerable sessions (#1321)
* feat: add generateWorld() activity and Reactor adapter

Add a session-based world generation activity, a first-party
@tanstack/ai-reactor adapter that mints scoped tokens for Orbis
and other Reactor world models, and a ts-react-world example app.

* feat: add reactorVideo adapter for live generateVideo sessions

Mint a scoped Reactor token from generateVideo() for Helios, FastH3,
Orbis, LongLive, and LTX. Live sessions return a token instead of a
download URL, so the browser connects with the js-sdk.

* feat: use BYOK in the ts-react-world example

Paste a Reactor key in the browser. The relay reads x-byok-reactor, then
REACTOR_API_KEY, and mints the session token.

* fix: unlock saved BYOK keys in the world example

Passkey storage locks the keyring after a reload. Show Unlock, treat
locked keys as saved, and call byok.prepare() before minting.

* fix: load Reactor wasm in the world example

Vite was pre-bundling @reactor-team/js-sdk, which broke the relative
import of dist/wasm/reactor_wasm.js. Exclude the SDK from optimizeDeps
and resolve that glue file from the package.

* fix: hydrate BYOK env and passkey peek like ts-react-chat

Call byok.ready() and getEnvKeyStatus() on mount so a refresh shows a
locked passkey key or REACTOR_API_KEY on the relay.

* fix: pre-bundle awaitqueue when the Reactor SDK is unbundled

Excluding @reactor-team/js-sdk from optimizeDeps left its CJS dependency
awaitqueue as a raw named import. Include it so Vite converts AwaitQueue.

* feat: add generateLive() and fold world into the media example

Mint live session tokens with generateLive(). reactorVideo and falLive
(H3 Max Director) use that activity. generateVideo stays the job path.

The media example now has Live and World tabs. The ts-react-world
example is removed.

* fix: play live video and open the key dialog on a missing key

Helios uses set_sr_scale, not set_resolution. Mute the video and
re-attach on unmute so Chrome shows frames. A missing key now
returns byokMissing and opens the header dialog. Seedance is a tab
on the media example. Sherif ignores the dual @fal-ai/client versions.

* fix: proxy fal Director auth and keep Orbis on World

WMA rejects a /tokens/ JWT as Key credentials. The media example opens
H3 Max Director through /api/fal/proxy with the real FAL_KEY. Orbis
stays on generateWorld only. Live and World share seed-image upload.

* ci: apply automated fixes

* fix: return the WMA app id from falLive and lock the proxy

generateLive() now puts fal-ai/minimax-h3-max-director on result.model
so wma(live.model) works. The example proxy only forwards WMA session
paths and Director /ice. Doc snippets type-check under kiira.

* refactor: rename generateLive to generateLiveVideo

The activity, adapter kind, fal factory, and createLive method now use
the LiveVideo name so they match generateLiveVideo().

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
History 2026-09-08 16:23:59 +10:00
..
ag-ui ci: Version Packages (#1314) 2026-09-03 18:57:55 +02:00
sandbox-cloudflare chore(examples): upgrade remaining apps to Vite 8 (#1159) 2026-08-20 11:06:06 +02:00
sandbox-web chore(deps): update build & test tooling (#1124) 2026-08-17 04:52:40 +00:00
ts-angular-chat chore(examples): upgrade remaining apps to Vite 8 (#1159) 2026-08-20 11:06:06 +02:00
ts-code-mode-web chore(deps): update build & test tooling (#1124) 2026-08-17 04:52:40 +00:00
ts-group-chat chore(examples): upgrade remaining apps to Vite 8 (#1159) 2026-08-20 11:06:06 +02:00
ts-react-chat feat(ai-cloudflare): add Cloudflare Workers AI and AI Gateway adapter (#1309) 2026-09-03 12:04:59 +02:00
ts-react-media feat: add generateWorld() and generateLiveVideo() for prompt-steerable sessions (#1321) 2026-09-08 16:23:59 +10:00
ts-react-native-chat chore(deps): update build & test tooling (#1124) 2026-08-17 04:52:40 +00:00
ts-react-rerank feat: add self-describing billed usage to TokenUsage (#896) 2026-08-19 11:48:53 +02:00
ts-react-search chore(deps): update build & test tooling (#1124) 2026-08-17 04:52:40 +00:00
ts-react-ui-chatbot feat(ui): add typed headless createChatUI adapters (#1252) 2026-09-01 15:58:34 +02:00
ts-remix-chat feat(ai-remix): add Remix 3 adapter and guitar chat example (#1289) 2026-09-02 11:10:30 +02:00
ts-solid-chat feat(ui): add typed headless createChatUI adapters (#1252) 2026-09-01 15:58:34 +02:00
ts-svelte-chat feat(ui): add typed headless createChatUI adapters (#1252) 2026-09-01 15:58:34 +02:00
ts-vue-chat feat(ui): add typed headless createChatUI adapters (#1252) 2026-09-01 15:58:34 +02:00
vanilla-chat chore(examples): upgrade remaining apps to Vite 8 (#1159) 2026-08-20 11:06:06 +02:00
README.md feat: add generateWorld() and generateLiveVideo() for prompt-steerable sessions (#1321) 2026-09-08 16:23:59 +10:00

TanStack AI Examples

This directory contains comprehensive examples demonstrating TanStack AI across multiple TypeScript frameworks.

Quick Start

Choose an example based on your use case:

TypeScript Examples

TanStack Chat (ts-react-chat)

A full-featured chat application built with the TanStack ecosystem.

Tech Stack:

  • TanStack Start (full-stack React framework)
  • TanStack Router (type-safe routing)
  • TanStack Store (state management)
  • @tanstack/ai (AI backend)
  • @tanstack/ai-react (React hooks)
  • @tanstack/ai-client (headless client)

Features:

  • ✅ Real-time streaming with OpenAI GPT-4o
  • ✅ Automatic tool execution loop
  • ✅ Rich markdown rendering
  • ✅ Conversation management
  • ✅ Modern UI with Tailwind CSS

Getting Started:

cd examples/ts-react-chat
pnpm install
cp env.example .env
# Edit .env and add your OPENAI_API_KEY
pnpm start

📖 Full Documentation


Group Chat (ts-group-chat)

A real-time multi-user chat application with AI integration, demonstrating WebSocket-based communication and TanStack AI.

Tech Stack:

  • TanStack Start (full-stack React framework)
  • TanStack Router (type-safe routing)
  • Cap'n Web RPC (bidirectional WebSocket RPC)
  • @tanstack/ai (AI backend)
  • @tanstack/ai-anthropic (Claude adapter)
  • @tanstack/ai-client (headless client)
  • @tanstack/ai-react (React hooks)

Features:

  • ✅ Real-time multi-user chat with WebSocket
  • ✅ Online presence tracking
  • ✅ AI assistant (Claude) integration with queuing
  • ✅ Message broadcasting to all users
  • ✅ Modern chat UI (iMessage-style)
  • ✅ Username-based authentication (no registration)

Getting Started:

cd examples/ts-group-chat
pnpm install
cp .env.example .env
# Edit .env and add your ANTHROPIC_API_KEY
pnpm dev

Open http://localhost:4000 in multiple browser tabs to test multi-user functionality.

Key Concepts:

  • WebSocket RPC: Uses Cap'n Web RPC for type-safe bidirectional communication
  • AI Queuing: Claude requests are queued and processed sequentially
  • Real-time Updates: Messages and online users update in real-time
  • Message Broadcasting: Server broadcasts messages to all connected clients

📖 Full Documentation


Reranking (ts-react-rerank)

A single-page app that reorders a fixed set of support articles by relevance to a query, showing the original order and the reranked order side by side.

Tech Stack:

  • TanStack Start (full-stack React framework)
  • @tanstack/ai (the rerank() activity)
  • @tanstack/ai-cohere (Cohere rerank adapter)
  • @tanstack/ai-openrouter (OpenRouter rerank adapter)

Features:

  • ✅ One rerank() call, two providers behind a dropdown
  • ✅ Object documents — the original typed object comes back on each result
  • ✅ Relevance scores and original position per document
  • ✅ topN control and search-unit usage reporting
  • ✅ API keys stay on the server

Getting Started:

cd examples/ts-react-rerank
pnpm install
cp .env.example .env
# Add COHERE_API_KEY and/or OPENROUTER_API_KEY — you only need the one you pick
pnpm dev

📖 Full Documentation


World generation (ts-react-media)

The media example has a World tab. Paste a Reactor key in the header dialog. The relay calls generateWorld(). The page connects and can steer the scene mid-run.

cd examples/ts-react-media
pnpm install
pnpm dev

Open the World tab. Paste a Reactor key, or set REACTOR_API_KEY.


Vanilla Chat

A framework-free chat application using pure JavaScript and @tanstack/ai-client.

Tech Stack:

  • Vanilla JavaScript (no frameworks!)
  • @tanstack/ai-client (headless client)
  • Vite (dev server)

Features:

  • ✅ Pure vanilla JavaScript
  • ✅ Real-time streaming messages
  • ✅ Beautiful, responsive UI
  • ✅ No framework dependencies

Getting Started:

cd examples/vanilla-chat
pnpm install
pnpm start

📖 Full Documentation


AG-UI Polyglot Echo (ag-ui)

A React SPA that connects to Go, Rust, PHP, Zig, Bash, and Python chat servers over the AG-UI SSE protocol, with each backend streaming OpenAI or Anthropic completions. Toolchain detection writes public/servers.json; unavailable backends show setup instructions in the UI.

Tech Stack:

  • React + Vite (SPA)
  • @tanstack/ai-react + @tanstack/ai-react/ui
  • Go chat server (net/http, :8001)
  • Rust chat server (Axum, :8002)
  • PHP chat server (built-in server + curl, :8003)
  • Zig chat server (stdlib HTTP, :8004)
  • Bash chat server (socat + curl + jq, :8005)
  • Python chat server (stdlib HTTP + urllib, :8006)

Features:

  • ✅ Backend picker (Go | Rust | PHP | Zig | Bash | Python)
  • ✅ Toolchain-gated dev:all + servers.json availability
  • ✅ Setup instructions when a runtime is missing (or disabled via AGUI_DISABLE_SERVERS)
  • ✅ Provider picker (OpenAI | Anthropic)
  • ✅ Hand-rolled AG-UI SSE in six languages
  • ✅ Streaming LLM responses via env API keys

Getting Started:

cd examples/ag-ui
pnpm install
cp .env.example .env
pnpm dev:all

Install whichever backends you want to run locally (Go, Rust, PHP, Zig, Bash, Python) plus provider API keys. The Bash server uses Bash 4+, curl, jq, and socat (brew install bash jq socat). To simulate missing runtimes: AGUI_DISABLE_SERVERS=php,zig,bash,python pnpm dev:all.

📖 Full Documentation


Architecture Patterns

Full-Stack TypeScript

Use TanStack AI end-to-end in TypeScript:

Frontend (React)
  ↓ (useChat hook)
@tanstack/ai-react
  ↓ (ChatClient)
@tanstack/ai-client
  ↓ (SSE/HTTP)
Backend (TanStack Start API Route)
  ↓ (chat() function)
@tanstack/ai
  ↓ (adapter)
AI Provider (OpenAI/Anthropic/etc.)

Example: TanStack Chat (ts-react-chat)

Common Patterns

Server-Sent Events (SSE) Streaming

All examples use SSE for real-time streaming:

Backend:

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

const stream = chat({
  adapter: openaiText(),
  model: 'gpt-4o',
  messages,
})

return toServerSentEventsResponse(stream)

Frontend:

import { ChatClient, fetchServerSentEvents } from '@tanstack/ai-client'

const client = new ChatClient({
  connection: fetchServerSentEvents('/api/chat'),
})

Automatic Tool Execution

The TypeScript backend (@tanstack/ai) automatically handles tool execution:

import { chat, toolDefinition } from '@tanstack/ai'
import { z } from 'zod'

// Step 1: Define the tool schema
const weatherToolDef = toolDefinition({
  name: 'getWeather',
  description: 'Get weather for a location',
  inputSchema: z.object({
    location: z.string().describe('The city and state, e.g. San Francisco, CA'),
  }),
  outputSchema: z.object({
    temp: z.number(),
    condition: z.string(),
  }),
})

// Step 2: Create server implementation
const weatherTool = weatherToolDef.server(async ({ location }) => {
  // This is called automatically by the SDK
  return { temp: 72, condition: 'sunny' }
})

const stream = chat({
  adapter: openaiText(),
  model: 'gpt-4o',
  messages,
  tools: [weatherTool], // SDK executes these automatically
})

Clients receive:

  • content chunks - text from the model
  • tool_call chunks - when the model calls a tool
  • tool_result chunks - results from tool execution
  • done chunk - conversation complete

Development Tips

Environment Variables

Each example has an env.example file. Copy it to .env and add your API keys:

OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...

Building for Production

pnpm build

Contributing

When adding new examples:

  1. Create a README.md with setup instructions
  2. Add an env.example file with required environment variables
  3. Document the tech stack and key features
  4. Include usage examples with code snippets
  5. Update this README to list your example

Learn More


Built with ❤️ by the TanStack community