Skip to main content
Chat is implemented in two hosts:
  • apps/web owns protected APIs, workspace authorization, storage signing, and private Supabase RPC execution.
  • apps/chat is the standalone satellite UI at https://chat.tuturuuu.com, with local Portless origin https://chat.tuturuuu.localhost and direct fallback http://localhost:7821.
Live delivery, the shared web and mobile experience, and integrations with Tasks, Calendar, Meet, Mail and Drive are specified in Chat realtime and integrations. Both hosts render the same shared UI from @tuturuuu/ui/chat/chat-workspace and call the same @tuturuuu/internal-api chat client. Do not add direct browser Supabase reads, client-local raw fetch('/api/...'), or host-specific chat business logic. Unmatched /api/workspaces/* requests, including workspace invitations and workspace details, use the Web fallback with their paths and queries intact. Local Chat API handlers retain precedence. Regression coverage lives in apps/chat/src/next-config.test.ts.

API Ownership

apps/chat forwards unmatched /api/* paths to apps/web through fallback rewrites, after local auth and build-info handlers. The more specific /api/v1/infrastructure/* fallback still targets Infrastructure first. The standalone app authenticates through the cross-app session target chat; apps/web accepts that target only on the chat API surface and still performs normal request-scoped user validation before protected work. Chat consumes /verify-token in the proxy before rendering pages. The verifier sets both the Chat-local app-session cookie and the Web-issued app-session cookie pair used by forwarded central APIs. Protected Chat routes require both cookies; if an older local-only cookie is present, Chat sends the user through the local /login recovery path so the platform handoff refreshes the full cookie pair. Access-token refresh must stay same-origin through /api/auth/refresh-app-session instead of sending the browser back to apps/web while a valid refresh cookie exists. Chat routes must follow this order:
  1. Authenticate the request user or app-session user.
  2. Normalize workspace aliases such as personal.
  3. Verify workspace membership and the required chat permission.
  4. Call private-schema RPCs through a service-role admin client with .schema('private').rpc(...).
  5. Return only JSON DTOs that are safe for participants of the conversation.

Private Schema Contract

Durable chat data lives in private tables only:
  • private.chat_conversations
  • private.chat_conversation_members
  • private.chat_messages
  • private.chat_message_attachments
  • private.chat_message_reactions
  • private.chat_conversation_ai_settings
  • private.chat_audit_events
The migration revokes private schema, table, and function access from public, anon, and authenticated; grants execution only to service_role; and keeps RLS enabled with no permissive direct-access policies. Browser clients must recover durable state by refetching the apps/web API, never by reading private or legacy public chat tables directly. RPCs enforce participant access, workspace membership, direct-chat uniqueness, attachment path ownership, message ownership, reaction limits, search scoping, read state, and audit metadata. When adding behavior, extend the private RPC surface first and keep the web route as a thin auth/validation wrapper.

Attachments

Chat attachments use the existing workspace storage provider rather than a separate chat bucket. The upload flow is:
  1. Client requests /api/v1/workspaces/:wsId/chat/conversations/:conversationId/attachments/upload-url.
  2. apps/web verifies the participant through private.chat_prepare_attachment.
  3. apps/web creates a Drive-backed signed upload payload under :wsId/chats/:conversationId/*.
  4. The client uploads to the signed URL and sends the returned attachment draft with the message.
  5. private.chat_send_message finalizes records and links attachments to the message.
Downloads use the attachment signing route. It calls private.chat_get_attachment first, then issues a short-lived storage read URL only after the caller is confirmed as a participant.

Permissions

The chat permission set is:
  • view_chat: read conversations where the actor is a participant.
  • create_chat: create direct messages, groups, channels, and AI chats.
  • manage_chat: manage chat membership, state, and AI settings.
  • moderate_chat: moderate messages and reactions.
Member defaults include view_chat and create_chat. The dashboard navigation should hide or disable Chat when view_chat is missing. GET /api/v1/workspaces/:wsId/chat/conversations returns canModerate: whether the viewer holds moderate_chat in that workspace, from the same public.has_workspace_permission check private.chat_delete_message applies to other members’ messages. Clients use it only to offer deleting others’ messages in the workspace’s own conversations; the delete RPC still enforces it, and a failed check reports false. Regression coverage: route.search.test.ts and lib/chat/moderation.test.ts in apps/web. Deleting a message runs the native delete first. The delete checks chat access to the workspace and conversation, that the message belongs to that conversation, and that the caller is its sender or holds moderate_chat (private.chat_delete_message). A refused native delete returns its error and nothing else happens. In a conversation bridged to a connected site, the route then asks the site to delete its copy. When that deletion cannot be delivered (no site mapping for the message, the binding unavailable, or the site refusing or timing out), the message stays deleted in the workspace, the message.deleted realtime event is still published, and the response is 200 with externalDelete: 'failed'; the site keeps its copy and nothing retries automatically. Conversations without a connected site return the plain { message } body. No web or mobile client surfaces externalDelete yet, so a failed site deletion is visible only in the server log (“Connected site did not accept a chat deletion”); surfacing and retrying it is tracked as GH-618. Regression coverage: route.delete.test.ts under chat/conversations/[conversationId]/messages/[messageId] in apps/web. The same route searches conversations when q is set (searchWorkspaceChatConversations in @tuturuuu/internal-api). It matches the conversation title and the display names and handles of members other than the viewer, ignoring case and accents (Vietnamese đ included), and needs two to 100 characters; shorter or longer queries are rejected with 400. It filters the viewer’s own listing (the actor-scoped chat_list_conversations RPC plus the AI sources the list already merges), so it cannot return a conversation the viewer could not list; results are newest first and paginate with limit and offset. External chat (scope=external) is not searchable. The search reads the viewer’s whole listing before filtering, which suits conversation counts that fit one listing. Regression coverage: route.search.test.ts and lib/chat/conversation-search.test.ts.

AI Conversations

AI conversations are normal private chat conversations with ai_enabled = true plus private AI settings. Existing AI audit or billing tables can remain for provider usage tracking, but human chat access must not depend on public AI chat tables. Persist generated assistant messages through private chat RPCs so conversation history, read state, attachments, and audit events remain in one protected chat domain.