# oshekharo/cyber-ai architecture

> Architecture diagram of the GitHub repository oshekharo/cyber-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/oshekharo/cyber-ai
- Repository: https://github.com/oshekharo/cyber-ai

## Overview

#### Purpose and Entry Points
Cyber AI is a React/TypeScript single-page cybersecurity assistant: authenticated users run multi-session AI chats; administrators manage Supabase users and roles. The browser bootstraps at `src/main.tsx`, renders application routing/state through `src/App.tsx`, and calls serverless admin APIs under `api/`. Vite builds the frontend; `vercel.json` supports Vercel deployment.

#### Application Shell and Access Control
`src/App.tsx` is the top-level gate: it selects authentication, chat workspace, or admin views based on auth state and role. `src/hooks/useAuth.ts` owns session lifecycle—sign-up, sign-in, sign-out, and user/role resolution—against the configured Supabase client in `src/lib/supabase.ts`. `src/components/AuthScreen.tsx` is the credential UI; `src/components/AdminDashboard.tsx` is restricted to administrators.

#### Chat State and Session Boundary
`src/hooks/useChat.ts` is the client-side chat domain layer. It manages conversation sessions, active-session selection, messages, streaming/loading state, and actions such as create/delete/rename/export/search. Per the design, sessions and conversations persist in browser `localStorage`, separating conversational state from Supabase-backed identity. The workspace composition is rooted at `src/components/ChatWorkspace.tsx`, with session navigation in `src/components/Sidebar.tsx`.

#### AI Request Flow
Message submission travels from `src/components/InputBar.tsx` through chat-state actions in `src/hooks/useChat.ts` to `src/api/chat.ts`. That client calls the configurable primary `/api/chat` backend (`VITE_API_URL`), which is external to this repository, and supports a Brave Search fallback using a browser-provided token. Returned/streamed assistant content is incorporated into the active local session and displayed through `src/components/MessageList.tsx` and `src/components/MessageBubble.tsx`.

#### User Interaction Layer
`src/components/ChatWorkspace.tsx` coordinates the responsive workspace: sidebar/session controls, message rendering, input, error state, and auxiliary search/command surfaces. `src/components/CommandPalette.tsx` exposes chat-oriented slash actions, while `src/components/MessageSearch.tsx` searches the current conversation. Rendering supports Markdown/GFM and syntax highlighting; message-level copy, feedback, and regeneration remain UI actions over the active chat state.

#### Identity, Database, and Privileged APIs
Supabase provides email/password authentication and PostgreSQL-backed authorization data. Its expected tables, role model, and RLS policies are defined in `supabase/schema.sql`. Browser code uses the anon-key Supabase client; privileged user enumeration, role update, and account deletion are isolated in `api/admin/users.ts`, which requires server-side `SUPABASE_SERVICE_ROLE_KEY`. This is the principal trust boundary: service-role credentials must never reach Vite client code.

#### Runtime and Deployment Boundaries
The frontend runs as a Vite SPA (React 19, TypeScript) and requires Node.js 18+ for development/build. Supabase is the managed auth/database service. `/api/chat` is an independently deployed AI backend, optionally hosted on Render; Brave Search is an optional external fallback. Vercel can serve the static app and `api/admin/users.ts` serverless endpoint, while deployment configuration resides in `vercel.json`.

Main entry points: [`src/App.tsx`](https://github.com/oshekharo/cyber-ai/blob/main/src/App.tsx) (App gate), [`src/components/ChatWorkspace.tsx`](https://github.com/oshekharo/cyber-ai/blob/main/src/components/ChatWorkspace.tsx) (Chat workspace), [`src/hooks/useChat.ts`](https://github.com/oshekharo/cyber-ai/blob/main/src/hooks/useChat.ts) (Chat domain state), [`src/api/chat.ts`](https://github.com/oshekharo/cyber-ai/blob/main/src/api/chat.ts) (AI request client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser SPA"]
  node_main["Browser bootstrap<br/>React entry<br/>[main.tsx]"]
  node_app["App gate<br/>routing and access control<br/>[App.tsx]"]
  node_auth["Auth lifecycle<br/>React hook<br/>[useAuth.ts]"]
  node_auth_screen["Auth screen<br/>credential UI<br/>[AuthScreen.tsx]"]
  node_chat_workspace["Chat workspace<br/>workspace composition<br/>[ChatWorkspace.tsx]"]
  node_chat["Chat domain state<br/>React hook<br/>[useChat.ts]"]
  node_local_storage[("Conversation storage<br/>browser localStorage")]
  node_sidebar["Session navigation<br/>chat UI<br/>[Sidebar.tsx]"]
  node_input["Message input<br/>chat UI<br/>[InputBar.tsx]"]
  node_messages["Message rendering<br/>chat UI<br/>[MessageList.tsx]"]
  node_message_bubble["Message actions<br/>message UI<br/>[MessageBubble.tsx]"]
  node_chat_api["AI request client<br/>browser API client<br/>[chat.ts]"]
  node_admin_dashboard["Admin dashboard<br/>restricted UI<br/>[AdminDashboard.tsx]"]
  node_supabase_client["Supabase client<br/>anon-key client<br/>[supabase.ts]"]
end

subgraph group_supabase["Supabase"]
  node_supabase_service[("Auth and PostgreSQL<br/>managed service")]
  node_schema["Role schema and RLS<br/>SQL schema<br/>[schema.sql]"]
end

subgraph group_external["External Services"]
  node_ai_backend{{"AI chat backend<br/>external API"}}
  node_brave{{"Brave Search<br/>optional fallback API"}}
end

subgraph group_vercel["Vercel"]
  node_admin_api{{"Admin users API<br/>serverless API<br/>[users.ts]"}}
  node_deployment["Deployment routing<br/>Vercel configuration<br/>[vercel.json]"]
end

node_main -->|"renders"| node_app
node_app -->|"uses"| node_auth
node_app -->|"shows when signed out"| node_auth_screen
node_app -->|"shows for users"| node_chat_workspace
node_app -->|"shows for admins"| node_admin_dashboard
node_auth_screen -->|"submits credentials"| node_auth
node_auth -->|"authenticates through"| node_supabase_client
node_supabase_client -->|"uses anon access"| node_supabase_service
node_schema -->|"defines"| node_supabase_service
node_chat_workspace -->|"coordinates"| node_chat
node_chat_workspace -->|"composes"| node_sidebar
node_chat_workspace -->|"composes"| node_input
node_chat_workspace -->|"composes"| node_messages
node_messages -->|"renders"| node_message_bubble
node_input -->|"submits message"| node_chat
node_chat -->|"persists sessions"| node_local_storage
node_chat -->|"sends prompt"| node_chat_api
node_chat_api -->|"calls /api/chat"| node_ai_backend
node_chat_api -.->|"fallback search"| node_brave
node_admin_dashboard -->|"admin operations"| node_admin_api
node_admin_api -->|"service-role access"| node_supabase_service
node_deployment -->|"deploys"| node_admin_api
node_deployment -->|"serves SPA"| node_main

click node_main "https://github.com/oshekharo/cyber-ai/blob/main/src/main.tsx"
click node_app "https://github.com/oshekharo/cyber-ai/blob/main/src/App.tsx"
click node_auth "https://github.com/oshekharo/cyber-ai/blob/main/src/hooks/useAuth.ts"
click node_auth_screen "https://github.com/oshekharo/cyber-ai/blob/main/src/components/AuthScreen.tsx"
click node_chat_workspace "https://github.com/oshekharo/cyber-ai/blob/main/src/components/ChatWorkspace.tsx"
click node_chat "https://github.com/oshekharo/cyber-ai/blob/main/src/hooks/useChat.ts"
click node_sidebar "https://github.com/oshekharo/cyber-ai/blob/main/src/components/Sidebar.tsx"
click node_input "https://github.com/oshekharo/cyber-ai/blob/main/src/components/InputBar.tsx"
click node_messages "https://github.com/oshekharo/cyber-ai/blob/main/src/components/MessageList.tsx"
click node_message_bubble "https://github.com/oshekharo/cyber-ai/blob/main/src/components/MessageBubble.tsx"
click node_chat_api "https://github.com/oshekharo/cyber-ai/blob/main/src/api/chat.ts"
click node_admin_dashboard "https://github.com/oshekharo/cyber-ai/blob/main/src/components/AdminDashboard.tsx"
click node_supabase_client "https://github.com/oshekharo/cyber-ai/blob/main/src/lib/supabase.ts"
click node_schema "https://github.com/oshekharo/cyber-ai/blob/main/supabase/schema.sql"
click node_admin_api "https://github.com/oshekharo/cyber-ai/blob/main/api/admin/users.ts"
click node_deployment "https://github.com/oshekharo/cyber-ai/blob/main/vercel.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app,node_auth,node_auth_screen,node_chat_workspace,node_chat,node_local_storage,node_sidebar,node_input,node_messages,node_message_bubble,node_chat_api,node_admin_dashboard,node_supabase_client toneBlue
class node_supabase_service,node_schema toneAmber
class node_ai_backend,node_brave toneMint
class node_admin_api,node_deployment toneRose
```

## Components

### Browser SPA

Vite-built React application.

- [Browser bootstrap](https://github.com/oshekharo/cyber-ai/blob/main/src/main.tsx): React entry · `src/main.tsx`
- [App gate](https://github.com/oshekharo/cyber-ai/blob/main/src/App.tsx): routing and access control · Selects auth, chat, or admin views by session and role. · `src/App.tsx`
- [Auth lifecycle](https://github.com/oshekharo/cyber-ai/blob/main/src/hooks/useAuth.ts): React hook · Manages sign-in, sign-up, sign-out, and role resolution. · `src/hooks/useAuth.ts`
- [Auth screen](https://github.com/oshekharo/cyber-ai/blob/main/src/components/AuthScreen.tsx): credential UI · `src/components/AuthScreen.tsx`
- [Chat workspace](https://github.com/oshekharo/cyber-ai/blob/main/src/components/ChatWorkspace.tsx): workspace composition · Coordinates sessions, messages, input, and auxiliary chat surfaces. · `src/components/ChatWorkspace.tsx`
- [Chat domain state](https://github.com/oshekharo/cyber-ai/blob/main/src/hooks/useChat.ts): React hook · Owns local sessions, active conversation, messages, and streaming state. · `src/hooks/useChat.ts`
- **Conversation storage**: browser localStorage · Persists sessions and conversations separately from identity.
- [Session navigation](https://github.com/oshekharo/cyber-ai/blob/main/src/components/Sidebar.tsx): chat UI · `src/components/Sidebar.tsx`
- [Message input](https://github.com/oshekharo/cyber-ai/blob/main/src/components/InputBar.tsx): chat UI · `src/components/InputBar.tsx`
- [Message rendering](https://github.com/oshekharo/cyber-ai/blob/main/src/components/MessageList.tsx): chat UI · Renders assistant and user messages with Markdown and syntax highlighting. · `src/components/MessageList.tsx`
- [Message actions](https://github.com/oshekharo/cyber-ai/blob/main/src/components/MessageBubble.tsx): message UI · Provides copy, feedback, and regeneration actions. · `src/components/MessageBubble.tsx`
- [AI request client](https://github.com/oshekharo/cyber-ai/blob/main/src/api/chat.ts): browser API client · Calls the configurable chat backend and supports Brave fallback. · `src/api/chat.ts`
- [Admin dashboard](https://github.com/oshekharo/cyber-ai/blob/main/src/components/AdminDashboard.tsx): restricted UI · `src/components/AdminDashboard.tsx`
- [Supabase client](https://github.com/oshekharo/cyber-ai/blob/main/src/lib/supabase.ts): anon-key client · `src/lib/supabase.ts`

### Supabase

Managed identity and authorization data.

- **Auth and PostgreSQL**: managed service
- [Role schema and RLS](https://github.com/oshekharo/cyber-ai/blob/main/supabase/schema.sql): SQL schema · Defines authorization tables, roles, and row-level policies. · `supabase/schema.sql`

### External Services

- **AI chat backend**: external API · Independently deployed endpoint addressed by VITE_API_URL.
- **Brave Search**: optional fallback API

### Vercel

Static hosting and serverless runtime.

- [Admin users API](https://github.com/oshekharo/cyber-ai/blob/main/api/admin/users.ts): serverless API · Uses service-role credentials for user and role administration. · `api/admin/users.ts`
- [Deployment routing](https://github.com/oshekharo/cyber-ai/blob/main/vercel.json): Vercel configuration · `vercel.json`

## Connections

- Browser bootstrap → App gate: renders
- App gate → Auth lifecycle: uses
- App gate → Auth screen: shows when signed out
- App gate → Chat workspace: shows for users
- App gate → Admin dashboard: shows for admins
- Auth screen → Auth lifecycle: submits credentials
- Auth lifecycle → Supabase client: authenticates through
- Supabase client → Auth and PostgreSQL: uses anon access
- Role schema and RLS → Auth and PostgreSQL: defines
- Chat workspace → Chat domain state: coordinates
- Chat workspace → Session navigation: composes
- Chat workspace → Message input: composes
- Chat workspace → Message rendering: composes
- Message rendering → Message actions: renders
- Message input → Chat domain state: submits message
- Chat domain state → Conversation storage: persists sessions
- Chat domain state → AI request client: sends prompt
- AI request client → AI chat backend: calls /api/chat
- AI request client → Brave Search: fallback search
- Admin dashboard → Admin users API: admin operations
- Admin users API → Auth and PostgreSQL: service-role access · Privileged server-side boundary.
- Deployment routing → Admin users API: deploys
- Deployment routing → Browser bootstrap: serves SPA
