Skip to main content
The ai-chat plugin adds an AI-powered chat interface to your Ship project. It features real-time streaming responses using Google Gemini, persistent chat history stored in MongoDB, and a polished chat UI with multiple conversation support.

Installation

npx create-ship-app@latest install ai-chat

What gets installed

Dependencies

WorkspacePackageVersion
apps/api@ai-sdk/google3.0.29
apps/apiai6.0.86
apps/web@ai-sdk/react3.0.88

Files

API (apps/api):
FileDescription
src/config/ai.config.tsEnvironment config for GOOGLE_GENERATIVE_AI_API_KEY
src/services/ai/ai.service.tsAI service using Google Gemini (gemini-2.5-flash) with streaming support
src/resources/ai-chat/ai-chat.schema.tsZod schema for chat conversations
src/resources/ai-chat/ai-chat.service.tsDatabase service for chat CRUD operations
src/resources/ai-chat/ai-message.schema.tsZod schema for individual messages (role, content)
src/resources/ai-chat/ai-message.service.tsDatabase service for message CRUD operations
src/resources/ai-chat/endpoints/create.tsCreate a new chat conversation
src/resources/ai-chat/endpoints/list.tsList all chats for the current user
src/resources/ai-chat/endpoints/delete.tsDelete a chat and its messages
src/resources/ai-chat/endpoints/get-messages.tsGet all messages in a chat
src/resources/ai-chat/endpoints/send-message.tsSend a message and stream the AI response via SSE
src/resources/ai-chat/index.tsBarrel export for services
Web (apps/web):
FileDescription
src/components/ui/prompt-input.tsxReusable prompt input component with auto-resize and keyboard shortcuts
src/pages/ai-chat/index.page.tsxMain AI chat page with conversation sidebar
src/pages/ai-chat/[chatId].page.tsxIndividual chat page (route handler)
src/pages/ai-chat/components/AiChatBox.tsxChat container with message list and input
src/pages/ai-chat/components/AiChatInput.tsxChat input with send button
src/pages/ai-chat/components/AiChatMessage.tsxIndividual message bubble (user/assistant)
src/pages/ai-chat/components/AiMessageSkeleton.tsxLoading skeleton for messages
src/pages/ai-chat/hooks/useAiChatManager.tsHook managing chat state, SSE streaming, and message history

Environment variables

API (apps/api)

VariableDescription
GOOGLE_GENERATIVE_AI_API_KEYGoogle AI API key from Google AI Studio

How it works

Streaming responses

The plugin uses Server-Sent Events (SSE) for real-time AI response streaming:
  1. The user sends a message via POST /:chatId/messages.
  2. The API saves the user message, loads the full conversation history, and calls Google Gemini.
  3. The response is streamed back chunk by chunk as SSE events.
  4. Once complete, the assistant message is saved to the database.
The frontend useAiChatManager hook handles parsing the SSE stream and updating the UI in real time.

Chat management

Each user can have multiple chat conversations. Chats are automatically titled based on the first message. All data is stored in MongoDB via Ship’s data service layer.

API endpoints

MethodPathDescription
GET/ai-chat/List all chats for the current user
POST/ai-chat/Create a new chat
DELETE/ai-chat/:chatIdDelete a chat and its messages
GET/ai-chat/:chatId/messagesGet messages for a chat
POST/ai-chat/:chatId/messagesSend a message and stream AI response