Within applications deploying embedded AI components or requiring stringent access validation prior to execution, the useAgentSession hook operates as the primary initialization interface. It handles secure one-time ticket exchanges, retrieves the current session state, and dictates the active conversational agent for the application instance.By interfacing with the /agent/session API endpoint, the hook ensures that subsequent operations executed via useAgentContext are fully authenticated and explicitly routed.
An optional, single-use authentication ticket mapped to a backend grant. When supplied, the hook exchanges this token sequentially before fetching session parameters. Omit or pass null if navigating a standard, persistent session.
When configuring Wacht embedded contexts utilizing backend-generated session tickets, the hook must process the one-time artifact before rendering the interface:
import { useAgentSession } from "@wacht/tanstack-router";function SecureEmbeddedContext({ authorizationTicket }: { authorizationTicket: string }) { const { ticketLoading, hasSession, sessionError } = useAgentSession(authorizationTicket); if (ticketLoading) { return ( <div className="flex animate-pulse items-center justify-center p-12 text-indigo-700 font-semibold tracking-wide"> Exchanging authorization token... </div> ); } if (sessionError) { return ( <div className="p-6 bg-red-50 border border-red-200 rounded-lg"> <h3 className="text-red-800 font-bold mb-2">Authentication Subsystem Error</h3> <p className="text-red-600 font-mono text-sm">{sessionError.message}</p> </div> ); } if (!hasSession) { return <div className="p-6 text-gray-500 italic text-center">Security context expired or rejected. Request new authorization parameters.</div>; } return ( <div className="p-8 border border-gray-200 rounded-xl bg-white shadow-md"> <h2 className="text-xl font-bold text-gray-900 border-b pb-4 mb-4">Secure Context Initialized</h2> <p className="text-gray-600 leading-relaxed"> The application is now permitted to initiate execution streams against local operations. </p> {/* TargetAgentSelector and Chat components mount below */} </div> );}