Skip to content

history

Thread history loader exposed as a Mastra custom API route.

Backed entirely by native Mastra: looks up the active agent by id, asks its Memory instance to recall a page of MastraDBMessages, and converts the result to AI SDK V5 UIMessages with the official toAISdkV5Messages helper from @mastra/ai-sdk/ui. No direct database reads.

The route is registered through historyRoute as a Mastra registerApiRoute so it sits in the same dispatcher pipeline as the standard agent routes. That means the MastraServer auth middleware (in ./server.ts) has already populated RequestContext with MASTRA_THREAD_ID_KEY and MASTRA_RESOURCE_ID_KEY by the time the handler runs - no cookie or user lookups happen here, and the session-cookie logic stays the single source of truth in server.ts.