-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Next.js 16 AI chat client using Better Auth. UI features branching message trees, Mermaid diagrams, and KaTeX. Real AI responses stream via any OpenAI-compatible AI provider/Vercel AI SDK, with support for reasoning tokens, files, and MCP servers.
- Chat UI with branching (editing creates siblings).
- Streaming responses with collapsible
<Thinking>tokens. - Functional file support (Images, PDFs, Text, XLSX) via MinIO/Presigned URLs. XLSX uses MCP file bridge (local staging, path injection, auto-re-upload).
- MCP tool support (stdio/HTTP); selectable per-message via dialog.
- Slash-commands (
/shortcut); prepends content to AI calls (hidden in UI). - PostgreSQL persistence (Server Actions).
- Projects (shared system prompts) and Assistants (personas).
- Knowledge bases (UI-only).
- Mermaid/KaTeX rendering plus Artifact side panel.
- Auth: Email/Pass, GitHub/Discord OAuth, Passkeys, TOTP 2FA.
- Session tracking (fingerprinting/revocation).
- Transactional emails via Postmark.
-
Artifacts/Canvas: Multi-view (Markdown, XLSX, HTML, Mermaid). AI triggers
manage_artifacttool; supports streaming, edits, and export. -
Advanced Editing: Edit messages using full
ChatInputcontrols (model picker, tools, attachments); settings are persisted in message metadata and recovered when navigating branches. -
Persistent Context: User artifact edits save to DB and re-inject into AI context as
tool-resulthistory.
- Framework: Next.js 16.2.4 (App Router, Turbopack)
- Language: TypeScript 5, React 19.2.5
- DB: PostgreSQL (Drizzle ORM v0.45.2)
- Auth: Better Auth v1.6.8 (Passkey/2FA)
-
Styling: Tailwind v4.2.4, Shadcn (Radix),
@base-ui/reactv1.4.1 - State: Zustand v5.0.12
- Forms: React Hook Form v7.73.1 + Zod v4.3.6
- Containers: Podman
- Storage: MinIO (AWS SDK v3.1036.0)
-
AI SDK: Vercel AI SDK v6.0.168,
@ai-sdk/openaiv3.0.53,@ai-sdk/mcpv1.0.36 -
MCP:
@modelcontextprotocol/sdkv1.29.0 -
Libraries: react-markdown v10.1.0, Mermaid v11.14.0, KaTeX v0.16.45, resizable-panels v4.10.0, Sonner v2.0.7, date-fns v4.1.0, unpdf v1.6.0, vaul v1.1.2, uuid v14.0.0, cmdk v1.1.1,
@blocknote/reactv0.49.0,xlsxv0.18.5,postmarkv4.0.7,pgv8.20.0 - MCP Languages (Optional): Python, Java, Kotlin, Go, Rust, C++ (for local servers)
PostgreSQL/Drizzle. Schemas in drizzle/schemas/. Types derived via InferSelectModel.
- Summary: Core profiles.
-
Columns:
id(PK);name/email(UNIQUE);email_verified(bool);image;two_factor_enabled;created_at/updated_at. -
Links: 1:N
session,account,passkey,chat,attachment,mcp_server,assistant,project; 1:1two_factor.
- Summary: Active sessions with fingerprints.
-
Columns:
id(PK);expires_at;token(UNIQUE);ip_address/user_agent;user_id(FK);created_at/updated_at. -
Links: N:1
user.
- Summary: OAuth/Credential hashes.
-
Columns:
id(PK);account_id;provider_id;password; tokens/expiry;scope;user_id(FK). -
Links: N:1
user. Unique(user_id, provider_id).
- Summary: TTL tokens for email/reset.
-
Columns:
id(PK);identifier(email);value;expires_at.
- Summary: TOTP secrets and backup codes.
-
Columns:
id(PK);secret;backup_codes(JSON);user_id(FK). -
Links: 1:1
user.
- Summary: WebAuthn credentials.
-
Columns:
id(PK);name;public_key;credential_id;counter;device_type;backed_up;transports;user_id(FK). -
Links: N:1
user.
- Summary: Chat groups with shared prompts.
-
Columns:
id(PK);user_id(FK);name;description;global_prompt;is_pinned; timestamps. -
Links: N:1
user. Soft ref fromchat.
- Summary: AI personas.
-
Columns:
id(PK);user_id(FK);name;description;prompt;avatar; timestamps. -
Links: N:1
user. Soft ref fromchat.
- Summary: Persisted sessions.
-
Columns:
id(PK);title;user_id(FK);project_id;assistant_id;current_leaf_id; timestamps. -
Links: N:1
user, 1:Nmessage.
- Summary: Tree-structured entries.
-
Columns:
id(PK);chat_id(FK);role(user/assistant/system);content;parent_id;metadata(JSON: tools/reasoning);created_at. -
Links: N:1
chat, 1:Nattachment.
- Summary: S3 files.
-
Columns:
id(PK);message_id(FK);user_id(FK);name;mime_type;size;key(UNIQUE S3);created_at. -
Links: N:1
message,user.
- Summary: stdio/HTTP tool configs.
-
Columns:
id(PK);user_id(FK);name;type;command/args/url/headers/env;enabled; timestamps. -
Links: N:1
user.
- Summary: Reusable slash-shortcuts.
-
Columns:
id(PK);user_id(FK);title;shortcut;content; timestamps. -
Links: N:1
user.
-
Chat Components(components/chat/)-
ChatInput: Textarea with model/prompt logic. Full file validation/upload (limits: 3 img, 5 total; sizes 2/20/50MB). FunctionalToolPickerDialog. Supports initial values and optional cancel/save modes (icon-only buttons) for message editing. -
ChatUI: Streaming handler viauseStreamResponse. Handles persistence, attachments, artifacts, and regeneration (request chain simplified;selectedResourcesremoved). -
ChatPageClient: Hydrates Zustand from SSR; rendersChatUI. -
MarkdownRenderer: Renders MD, KaTeX, and Mermaid. -
Message Sub-components:ToolCallDisplay,ThinkingDisplay,StreamingPlaceholder,MessageActions(Edit/Delete/Regenerate/Branching). -
MessageBubble: Decomposed with extracted metadata parsing (lib/chat/parse-message-metadata.ts) and message-focused sub-components (components/chat/message/*). -
ArtifactPanel: Tabbed right-panel (Markdown, XLSX, HTML, Mermaid). Persistent side-by-side onmd+.
-
-
Chat Utils(lib/chat/)-
reconstruct-thread.ts: Linearizes message tree from leaf to root for rendering. -
get-deepest-leaf.ts: Finds terminal nodes by following the last child at each level.
-
-
Shared Components(components/shared/)-
ResourceListPage: Searchable grid for lists. -
DeleteConfirmDialog/RenameDialog: Common entity actions. -
ErrorPage/NotFoundPage: Reusable route-level wrappers for error and not-found states. -
AuthenticatedLayout: Guards routes; provides sidebar and breadcrumbs. -
ResponsiveMenu: DesktopDropdown/ MobileDrawer. -
SidebarTabs: Specialized layout wrapper for Shadcn UI tabs providing a vertical, right-aligned sidebar design for detail pages (Projects, Assistants, MCP Servers).
-
-
MCP/Assistant/Project/Prompt Components: CRUD UIs, server detail pages, shared MCP server form fields (components/mcp/server-form-fields.tsx), and slash-command palettes (cmdk). -
Store(lib/store/index.ts)- Zustand monolithic store with domain slices. Large slices (e.g.,
EntitySlice) are decomposed into dedicated files per function within domain-specific subdirectories (e.g.,lib/store/slices/entity/projects/). Actions manage optimistic state synced via DB-backed functions (createProjectDb, etc.). Mappers handle DB-to-store conversion.
- Zustand monolithic store with domain slices. Large slices (e.g.,
-
Auth / Schemas / Actions: Better Auth config, Zod validation for all entities (centralized inschemas/), and ownership-guarded Server Actions for persistence. -
MCP Library(lib/mcp/)- Decomposed into dedicated files per function. Includes tool discovery (
discover-tools-and-resources.ts), transport building (build-transport.ts), server actions (list-mcp-servers.ts,create-mcp-server.ts, etc.), and File Bridge (download-attachments-to-temp.ts,persist-modified-files.ts, andcleanup-temp-dir.ts). Connects LLM to local staged spreadsheets with SSRF protection and timeout safety.
- Decomposed into dedicated files per function. Includes tool discovery (
-
Storage / Attachments: AWS SDK S3/MinIO helpers. Client-side file processing, MIME resolution, and PDF/Text extraction. -
Hooks:useStreamResponse(AI loop),useAutoExpandingTextarea(shared textarea behavior),usePromptCommands(slash-commands),useEntityOptions(shared CRUD logic). -
Types(types/)-
artifact.ts:ArtifactDatainterface for multi-view artifacts. -
tool-call.ts:ToolCallStateinterface for streaming tool execution. -
message.ts: CoreMessageand tree structure types. -
discovered-tool.ts,discovered-resource.ts,mcp-connection.ts: Centralized MCP types. -
bridged-file.ts,file-bridge-result.ts,modified-attachment.ts: File bridge metadata and result types. - Domain types for
Chat,Assistant,Project,Attachment, etc.
-
App Router: (main)/ (Auth-guarded/Sidebar), auth/ (Public), api/auth/ (Better Auth).
Browser -> App Router -> Server Actions/API -> DB (Drizzle) / S3 (MinIO) / AI (User's Selected Provider).
- Zustand + PG: Optimistic UI with reliable persistence.
-
Message Tree:
parentIdlinks allow branching;currentLeafIdtracks active path. - Upload Order: Messages persist before S3 uploads to ensure ownership.
- ID Stability: Client-generated UUIDs persist to DB for stable file bridging.
- Prompt Injection: Project/Assistant prompts prepended server-side in API.
- GPT OSS Default: Natively supports tool calling for MCP.
-
Excel MCP: Uses Python
excel-mcp. Staging via/tmp/[uuid]withEXCEL_MCP_ALLOWED_DIRSoverrides. -
SSE Protocol: Events for
text,reasoning,tool-call,tool-result, andfile-modified. -
Artifacts: Internal
manage_artifacttool. User edits re-validate paths and update DB metadata for AI context re-injection. -
SDK v6: Strict compliance using
{ type: "json", value: ... }for tool outputs. -
Import Strategy: Avoid barrel files (
index.ts) for modules containing Server Actions or server-only logic to prevent bundling issues in Client Components. Use direct file imports instead. -
Schema Centralization: API request schemas (e.g.,
chatRequestSchema) and internal tool schemas (e.g.,manageArtifactSchema) are centralized inschemas/chat.tsto maintain consistency across the pipeline. -
Request Payload Simplification:
selectedResourceswas removed from the chat request schema and client call chain. -
MCP Connection Cleanup: MCP connection setup now performs client cleanup on connection failures (
lib/mcp/connect-server.ts). - Vertical Tabs: Right-aligned vertical tab pattern for detail/settings pages (Projects, Assistants, MCP Servers) to improve focus and navigation. Projects use decomposed tabs (e.g., separating "Prompt" from "Settings").
- Editing Metadata Persistence: Model and tool selections are persisted in the user message's metadata JSON, allowing for exact context recovery and configuration consistency when editing or branching from an older message.
-
BetterAuthActionButton: Normalises Better Auth mutation responses with toasts. -
ActionButton: GeneraluseTransitionwrapper for async tasks.
-
DB:
db:migrate,auth:generate,db:generate,db:push,db:studio. -
App:
podman compose up -d(Infra),npm run dev(Turbopack),npm run build.