import { useEffect, useRef, useState } from 'react' import { createFileRoute } from '@tanstack/react-router' import { fetchServerSentEvents, useChat } from '@tanstack/ai-react' import { clientTools } from '@tanstack/ai-client' import { ThinkingPart } from '@tanstack/ai-react/ui' import ReactMarkdown from 'react-markdown' import rehypeRaw from 'rehype-raw' import rehypeSanitize from 'rehype-sanitize' import rehypeHighlight from 'rehype-highlight' import remarkGfm from 'remark-gfm' import { MessageSquare, Plus, Square, Trash2 } from 'lucide-react' import type { ChatClientPersistence, ChatPersistedState, UIMessage, } from '@tanstack/ai-client' import GuitarRecommendation from '@/components/example-GuitarRecommendation' import { addToCartToolDef, addToWishListToolDef, getPersonalGuitarPreferenceToolDef, recommendGuitarToolDef, } from '@/lib/guitar-tools' // --------------------------------------------------------------------------- // Client tool implementations (same set the index route uses). The // /api/tanchat endpoint always merges the guitar *server* tools, and its // system prompt forces a `recommendGuitar` call — a *client* tool — so the // agent loop only completes if we provide these client implementations. // Reasoning + tool-call parts both live on the persisted UIMessage, so they // round-trip through the persistence adapter for free. // --------------------------------------------------------------------------- const getPersonalGuitarPreferenceToolClient = getPersonalGuitarPreferenceToolDef.client(() => ({ preference: 'acoustic' })) const addToWishListToolClient = addToWishListToolDef.client((args) => { const wishList = JSON.parse(localStorage.getItem('wishList') || '[]') wishList.push(args.guitarId) localStorage.setItem('wishList', JSON.stringify(wishList)) return { success: true, guitarId: args.guitarId, totalItems: wishList.length } }) const addToCartToolClient = addToCartToolDef.client((args) => ({ success: true, cartId: 'CART_CLIENT_' + Date.now(), guitarId: args.guitarId, quantity: args.quantity, totalItems: args.quantity, })) const recommendGuitarToolClient = recommendGuitarToolDef.client(({ id }) => ({ id: +id, })) const tools = clientTools( getPersonalGuitarPreferenceToolClient, addToWishListToolClient, addToCartToolClient, recommendGuitarToolClient, ) export const Route = createFileRoute('/threads')({ component: ThreadsRoute, }) // --------------------------------------------------------------------------- // Storage layer // // Two concerns, two key spaces — this is the whole point of the demo: // // 1. Per-thread message history → the PR's persistence adapter, keyed by the // ChatClient `id`. We namespace it under `THREAD_KEY_PREFIX + id` so every // thread is its own localStorage entry and threads never overwrite each // other. // 2. The thread catalog (which threads exist, their titles, recency) → a // separate index the example owns. The persistence adapter is deliberately // single-conversation and knows nothing about "all my chats", so listing // and ordering threads is an app-level layer built on top. // --------------------------------------------------------------------------- const INDEX_KEY = 'tanstack-ai:threads' const THREAD_KEY_PREFIX = 'tanstack-ai:thread:' const NEW_CHAT_TITLE = 'New chat' const TITLE_MAX = 40 interface ThreadMeta { id: string title: string updatedAt: number } const hasWindow = () => typeof window !== 'undefined' /** * Per-thread history adapter. Each thread's messages live under their own * namespaced key, so this satisfies the PR's `ChatClientPersistence` contract * (get/set/remove by id) while keeping threads isolated from each other. */ const threadPersistence: ChatClientPersistence = { getItem: (id) => { if (!hasWindow()) return null const raw = window.localStorage.getItem(THREAD_KEY_PREFIX + id) if (!raw) return null // `UIMessage.createdAt` is a Date that JSON.stringify turned into a string — // revive it on read. const state = JSON.parse(raw) as ChatPersistedState return { ...state, messages: state.messages.map((message) => ({ ...message, createdAt: typeof message.createdAt === 'string' ? new Date(message.createdAt) : message.createdAt, })), } }, setItem: (id, state) => { if (!hasWindow()) return window.localStorage.setItem(THREAD_KEY_PREFIX + id, JSON.stringify(state)) }, removeItem: (id) => { if (!hasWindow()) return window.localStorage.removeItem(THREAD_KEY_PREFIX + id) }, } function readIndex(): Array { if (!hasWindow()) return [] try { const raw = window.localStorage.getItem(INDEX_KEY) return raw ? (JSON.parse(raw) as Array) : [] } catch { return [] } } function writeIndex(threads: Array): void { if (!hasWindow()) return try { window.localStorage.setItem(INDEX_KEY, JSON.stringify(threads)) } catch { // Best-effort, mirroring the persistence adapter: a full or unavailable // store should never break the chat. } } const byRecency = (a: ThreadMeta, b: ThreadMeta) => b.updatedAt - a.updatedAt function truncateTitle(text: string): string { const trimmed = text.trim() return trimmed.length > TITLE_MAX ? `${trimmed.slice(0, TITLE_MAX).trimEnd()}…` : trimmed } function relativeTime(ts: number): string { const seconds = Math.floor((Date.now() - ts) / 1000) if (seconds < 60) return 'just now' const minutes = Math.floor(seconds / 60) if (minutes < 60) return `${minutes}m ago` const hours = Math.floor(minutes / 60) if (hours < 24) return `${hours}h ago` return `${Math.floor(hours / 24)}d ago` } /** * Owns the thread catalog in localStorage. This is the layer the persistence * adapter doesn't provide: the list of conversations, their titles, and recency. */ function useThreadIndex() { const [threads, setThreads] = useState>([]) // SSR-safety: the server has no localStorage, so we render an empty list and // load the real catalog on the client after mount. `loaded` gates bootstrap // logic so we don't auto-create a spurious thread before the real one loads. const [loaded, setLoaded] = useState(false) // Mirror of the catalog read synchronously by mutators. State closures are // stale between two calls in the same event handler (e.g. set-title then // bump-recency), which would make the second call clobber the first; the ref // is always current, so sequential mutations compose correctly. const threadsRef = useRef>([]) useEffect(() => { const initial = readIndex().sort(byRecency) threadsRef.current = initial setThreads(initial) setLoaded(true) }, []) const commit = (next: Array) => { const sorted = [...next].sort(byRecency) threadsRef.current = sorted setThreads(sorted) writeIndex(sorted) } const createThread = (): ThreadMeta => { const thread: ThreadMeta = { id: crypto.randomUUID(), title: NEW_CHAT_TITLE, updatedAt: Date.now(), } commit([thread, ...threadsRef.current]) return thread } const deleteThread = (id: string) => { threadPersistence.removeItem(id) commit(threadsRef.current.filter((t) => t.id !== id)) } /** Bump recency, and set the title the first time a thread gets one. */ const touchThread = (id: string, title?: string) => { commit( threadsRef.current.map((t) => t.id === id ? { ...t, updatedAt: Date.now(), title: title && t.title === NEW_CHAT_TITLE ? truncateTitle(title) : t.title, } : t, ), ) } return { threads, loaded, createThread, deleteThread, touchThread } } // --------------------------------------------------------------------------- // Route // --------------------------------------------------------------------------- function ThreadsRoute() { const { threads, loaded, createThread, deleteThread, touchThread } = useThreadIndex() const [activeId, setActiveId] = useState(null) // Ensure there's always an active thread: keep the current one if it still // exists, otherwise fall back to the most recent, otherwise create one. useEffect(() => { if (!loaded) return if (activeId && threads.some((t) => t.id === activeId)) return if (threads.length > 0) { setActiveId(threads[0].id) return } setActiveId(createThread().id) // `threads`/`createThread` are derived from the same state; depending on // `activeId` + `loaded` is enough to re-run when the active thread vanishes. }, [loaded, activeId, threads.length]) const handleNewChat = () => { // Don't pile up empty chats — if an untouched "New chat" exists, focus it. const empty = threads.find((t) => t.title === NEW_CHAT_TITLE) setActiveId(empty ? empty.id : createThread().id) } const handleDelete = (id: string) => { deleteThread(id) if (id === activeId) { const next = threads.find((t) => t.id !== id) // If nothing remains the bootstrap effect creates a fresh thread. setActiveId(next ? next.id : null) } } return (
{activeId ? ( touchThread(activeId, text)} onActivity={() => touchThread(activeId)} /> ) : (
Loading…
)}
) } // --------------------------------------------------------------------------- // Chat panel — one ChatClient per thread, hydrated from + saved to the adapter. // Remounted via `key={threadId}` on the parent so switching threads is a clean // swap; useChat then hydrates the newly-keyed thread's history. Reasoning // (`thinking`) and `tool-call` parts live on the persisted UIMessage, so they // are restored on reload / thread-switch alongside the text. Hardcoded to // OpenRouter GPT-5.1 because the /api/tanchat endpoint enables a reasoning // summary for the openrouter provider, so reasoning parts actually stream. // --------------------------------------------------------------------------- const CHAT_BODY = { provider: 'openrouter', model: 'openai/gpt-5.1' } as const const REHYPE_PLUGINS = [rehypeRaw, rehypeSanitize, rehypeHighlight] const REMARK_PLUGINS = [remarkGfm] /** Render a single UIMessage part: reasoning, text, or guitar card. Approval * prompts are surfaced separately from the `interrupts` array (see ThreadChat). */ function MessagePart({ part, index, message, }: { part: UIMessage['parts'][number] index: number message: UIMessage }) { if (part.type === 'thinking') { // "Complete" once any text part follows it in the same message. const isComplete = message.parts .slice(index + 1) .some((p) => p.type === 'text') return ( ) } if (part.type === 'text' && part.content) { return (
{part.content}
) } if ( part.type === 'tool-call' && part.name === 'recommendGuitar' && part.output ) { return } return null } function ThreadChat({ threadId, onFirstMessage, onActivity, }: { threadId: string onFirstMessage: (text: string) => void onActivity: () => void }) { const { messages, sendMessage, isLoading, error, interrupts, resuming, stop, } = useChat({ threadId, connection: fetchServerSentEvents('/api/tanchat'), persistence: threadPersistence, tools, forwardedProps: CHAT_BODY, onFinish: () => onActivity(), }) const [input, setInput] = useState('') const handleSubmit = (e: React.FormEvent) => { e.preventDefault() const text = input.trim() if (!text || isLoading) return if (messages.length === 0) onFirstMessage(text) onActivity() setInput('') void sendMessage(text) } return ( <>
{messages.length === 0 && (

Send a message to start this chat.

Try “Recommend me an acoustic guitar” to see reasoning + tool calls — then reload or switch threads and they're restored from localStorage.

)} {messages.map((message) => (
{message.role}
{message.parts.map((part, index) => ( ))}
))} {interrupts.map((interrupt) => interrupt.kind === 'tool-approval' ? (

🔒 Approval required: {interrupt.toolName}

                  {JSON.stringify(interrupt.originalArgs, null, 2)}
                
) : null, )} {error && (
{error.message}
)}
setInput(e.target.value)} placeholder="Try “Recommend me an acoustic guitar”…" className="flex-1 rounded-lg border border-gray-700 bg-gray-800 px-4 py-2 text-white placeholder-gray-500 focus:border-orange-500/50 focus:outline-none" /> {isLoading ? ( ) : ( )}
) }