AI applications
Stash AI
Multi-model chat application
Stash AI lets you switch between AI models in a single chat window.
Role Independent developer
Tools and technologies
- Next.js
- TypeScript
- React
- Vercel AI SDK
- OpenRouter
- Better Auth
- Prisma
- PostgreSQL
- TanStack Query
Why I built it
I wanted a chat interface where I could switch models mid-conversation and keep the existing history. That meant handling two different lifecycles: the response is streamed to the browser while it is being generated, and the finished turn needs to survive reopening the chat. I built the model selector, streaming route, and Prisma-backed chat history around those two jobs.
From model selection to a saved response
- Model selector and chat viewThe browser loads the model catalog and sends the selected model with the conversation messages.
- Next.js chat routeThe Vercel AI SDK converts the UI messages and starts an OpenRouter stream.
- React chat interfaceThe response is rendered incrementally as AI SDK message parts arrive.
- PostgreSQLAt completion, Prisma saves the message parts, roles, and selected model.
OpenRouter gives the chat route one way to invoke models from multiple providers. The catalog endpoint fetches model metadata and currently filters the list to models with zero listed prompt and completion cost. Selecting a model changes the model ID for the next request. The chat can continue to send the same conversation history as context.
Keeping streaming and history consistent
For the first turn, the create-chat action saves the chat and its initial user message together, which gives the stream an existing conversation to attach to. Follow-up turns use the AI SDK chat hook to submit messages, render streamed output, and offer a stop or regenerate action. The persistence code can skip writing the user message when it already exists, such as on regeneration. Message IDs also let the database skip duplicate inserts.
I stored message parts as serialized JSON along with the role and model for the turn. That preserves the shape used by the streaming interface when a response is saved. A message reader also accommodates older plain-text entries. The history actions scope lookups and deletes to the signed-in user, and the sidebar queries saved conversations independently of the model stream.