# vishal34sd/smart-cli-based-agent architecture

> Architecture diagram of the GitHub repository vishal34sd/smart-cli-based-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vishal34sd/smart-cli-based-agent
- Repository: https://github.com/vishal34sd/smart-cli-based-agent

## Overview

1. **Repository shape**
   This is a two-part monorepo: a **Next.js client** in `client/` and a **Node.js/Express backend + CLI runtime** in `server/`. The product is an AI terminal assistant called **Orbital CLI**, with the web app acting as the login/device-flow surface and the backend powering authentication, AI access, and CLI-related APIs.

2. **Client subsystem**
   `client/` is a Next.js App Router application. Its routes are split into:
   - `app/(auth)/sign-in` for authentication UI
   - `app/approve` and `app/device` for device authorization / approval states
   - `app/page.tsx` as the landing page
   The client is mostly presentation-oriented and uses reusable UI components under `client/components/ui` plus app-specific components like `LoginForm`.

3. **Frontend responsibilities**
   The client’s main job is to support the **OAuth/device authorization login flow** and provide status/approval screens. It also contains an `auth-client` helper and `config/api.ts`, which suggest the frontend talks to the backend over HTTP rather than embedding auth logic locally.

4. **Backend API boundary**
   `server/src/index.js` is the backend entry point, with `routes/` exposing HTTP endpoints:
   - `authRoutes.js` for login/auth/session/device-flow related actions
   - `cliRoutes.js` for CLI-facing operations
   Controllers in `src/controllers/` translate requests into service calls, keeping transport concerns separate from business logic.

5. **Authentication and identity flow**
   Authentication is implemented in `server/src/lib/auth.js`, `token.js`, `credentialStore.js`, and `middleware/auth.js`. The README and migrations indicate **device authorization** support, so the typical flow is:
   - user starts login from CLI or web UI
   - backend issues/handles a device code
   - user approves in the browser
   - backend issues a token/session used by CLI requests
   Sensitive values are stored via OS credential storage, not plaintext files.

6. **CLI subsystem**
   `server/src/cli/` is the command-line application layer. It uses Commander-style command modules under:
   - `commands/auth` for login/logout/about-me
   - `commands/config` for setting the API key
   - `commands/ai` for wakeup/chat actions
   - `commands/General` for search/open/play utilities
   `main.js` is the CLI entrypoint, and `utils/apiClient.js` is the bridge from CLI commands to the backend API.

7. **AI and agentic capabilities**
   AI behavior is split across `src/services/aiService.js`, `src/service/chatService.js`, `src/services/chatService.js`, and CLI-side helpers under `src/cli/ai` and `src/cli/chat`. These modules handle:
   - chat responses
   - streaming/interactive conversation
   - tool calling / agent mode
   - Google Gemini integration via config files like `googleConfig.js`
   The architecture keeps model interaction in service layers rather than directly in command handlers.

8. **Tools and action execution**
   Orbital’s “tool calling” and “agentic mode” are implemented as a separate concern from plain chat. Tool definitions/config live in `src/config/toolConfig.js` and `agentConfig.js`, while command implementations such as `searchYoutube`, `openApp`, and `playSong` represent executable capabilities. This means the assistant can both answer and perform local actions.

9. **Persistence layer**
   The backend uses **Prisma** with a relational database, likely Neon PostgreSQL per the README. Prisma schema and migrations exist in both `server/prisma/` and `server/src/prisma/`, indicating database-backed state for auth/device flow and conversation/message data. `src/db/prisma.js` and `src/lib/db.js` are the DB access boundary.

10. **Configuration and secrets**
   Environment/config management is centralized in `src/config/env.js`, `api.js`, `googleConfig.js`, and `orbitalConfig.js`. The system distinguishes between:
   - backend runtime config
   - Google Gemini settings
   - API base URLs
   - stored user API keys
   This is important for architecture because the app spans terminal, browser, and cloud-deployed services.

11. **Error handling and middleware**
   Cross-cutting concerns are isolated in `src/middleware/errorHandler.js` and `src/middleware/auth.js`. This suggests a conventional Express pipeline where auth is enforced before controller execution and errors are normalized centrally.

12. **Data flow summary**
   A typical end-to-end flow is:
   - CLI command triggers a backend API call
   - backend validates auth/session and loads persisted state from Prisma/Postgres
   - AI/service layer calls Gemini or executes a tool
   - results stream back to the CLI, or the browser UI updates for login/approval steps
   This architecture cleanly separates **terminal UX**, **web login UX**, **API/business logic**, and **persistence/integration** boundaries.

Main entry points: [`server/src/lib/auth.js`](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/auth.js) (Auth core), [`server/src/services/aiService.js`](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/services/aiService.js) (AI services), [`client/app/(auth)/sign-in/page.tsx`](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/app/(auth)/sign-in/page.tsx) (Auth screens), [`server/src/routes`](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/routes) (HTTP routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_app["Next.js app<br/>web client"]
  node_client_auth_pages["Auth screens<br/>route pages<br/>[page.tsx]"]
  node_client_landing["Landing page<br/>route page<br/>[page.tsx]"]
  node_client_login_form["Login form<br/>ui component<br/>[LoginForm.tsx]"]
  node_client_auth_client["Auth client<br/>browser auth helper<br/>[auth-client.ts]"]
  node_client_api_config["API config<br/>client config<br/>[api.ts]"]
end

subgraph group_server["Server"]
  node_server_entry["Server entry<br/>express bootstrap<br/>[index.js]"]
  node_server_routes["HTTP routes<br/>api routes"]
  node_server_auth_routes["Auth routes<br/>route module<br/>[authRoutes.js]"]
  node_server_cli_routes["CLI routes<br/>route module<br/>[cliRoutes.js]"]
  node_server_controllers["Controllers<br/>request handlers"]
  node_server_auth_lib["Auth core<br/>identity logic<br/>[auth.js]"]
  node_server_credential_store["Credential store<br/>secure storage<br/>[credentialStore.js]"]
  node_server_token["Token logic<br/>session/token<br/>[token.js]"]
  node_server_middleware["Middleware"]
  node_server_cli["CLI runtime<br/>terminal app<br/>[main.js]"]
  node_server_api_client["API client<br/>cli transport<br/>[apiClient.js]"]
  node_server_cli_commands["CLI commands<br/>command set"]
  node_server_ai_services["AI services<br/>model orchestration<br/>[aiService.js]"]
  node_server_tool_config["Tool config<br/>agent config<br/>[toolConfig.js]"]
  node_server_db[("Database layer<br/>persistence<br/>[prisma.js]")]
end

node_client_auth_pages -->|"renders"| node_client_login_form
node_client_auth_pages -->|"calls"| node_client_auth_client
node_client_auth_client -->|"uses"| node_client_api_config
node_client_app -->|"contains"| node_client_auth_pages
node_client_app -->|"contains"| node_client_landing
node_server_entry -->|"mounts"| node_server_routes
node_server_entry -->|"applies"| node_server_middleware
node_server_routes -->|"includes"| node_server_auth_routes
node_server_routes -->|"includes"| node_server_cli_routes
node_server_auth_routes -->|"delegates"| node_server_controllers
node_server_cli_routes -->|"delegates"| node_server_controllers
node_server_controllers -->|"uses"| node_server_auth_lib
node_server_auth_lib -->|"depends on"| node_server_token
node_server_auth_lib -->|"persists via"| node_server_credential_store
node_server_auth_lib -->|"reads/writes"| node_server_db
node_server_cli -->|"calls"| node_server_api_client
node_server_cli -->|"loads"| node_server_cli_commands
node_server_cli_commands -->|"invokes"| node_server_ai_services
node_server_ai_services -->|"uses"| node_server_tool_config
node_server_ai_services -->|"persists via"| node_server_db
node_server_ai_services -->|"protected by"| node_server_middleware
node_client_auth_pages -->|"approves via"| node_server_auth_routes
node_server_cli_commands -->|"requests"| node_server_cli_routes

click node_client_app "https://github.com/vishal34sd/smart-cli-based-agent/tree/main/client/app"
click node_client_auth_pages "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/app/(auth)/sign-in/page.tsx"
click node_client_landing "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/app/page.tsx"
click node_client_login_form "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/components/LoginForm.tsx"
click node_client_auth_client "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/lib/auth-client.ts"
click node_client_api_config "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/config/api.ts"
click node_server_entry "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/index.js"
click node_server_routes "https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/routes"
click node_server_auth_routes "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/routes/authRoutes.js"
click node_server_cli_routes "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/routes/cliRoutes.js"
click node_server_controllers "https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/controllers"
click node_server_auth_lib "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/auth.js"
click node_server_credential_store "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/credentialStore.js"
click node_server_token "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/token.js"
click node_server_middleware "https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/middleware"
click node_server_cli "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/cli/main.js"
click node_server_api_client "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/cli/utils/apiClient.js"
click node_server_cli_commands "https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/cli/commands"
click node_server_ai_services "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/services/aiService.js"
click node_server_tool_config "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/config/toolConfig.js"
click node_server_db "https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/db/prisma.js"

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_client_app,node_client_auth_pages,node_client_landing,node_client_login_form,node_client_auth_client,node_client_api_config toneBlue
class node_server_entry,node_server_routes,node_server_auth_routes,node_server_cli_routes,node_server_controllers,node_server_auth_lib,node_server_credential_store,node_server_token,node_server_middleware,node_server_cli,node_server_api_client,node_server_cli_commands,node_server_ai_services,node_server_tool_config,node_server_db toneAmber
```

## Components

### Client

Next.js app for sign-in and device approval

- [Next.js app](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/client/app): web client · App Router frontend for login and approval flows · `client/app`
- [Auth screens](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/app/(auth)/sign-in/page.tsx): route pages · Sign-in, approve, and device pages · `client/app/(auth)/sign-in/page.tsx`
- [Landing page](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/app/page.tsx): route page · Top-level client entry page · `client/app/page.tsx`
- [Login form](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/components/LoginForm.tsx): ui component · Reusable sign-in UI · `client/components/LoginForm.tsx`
- [Auth client](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/lib/auth-client.ts): browser auth helper · Frontend helper for backend auth requests · `client/lib/auth-client.ts`
- [API config](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/client/config/api.ts): client config · Backend base URL and client API settings · `client/config/api.ts`

### Server

Express API, CLI runtime, AI, and persistence

- [Server entry](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/index.js): express bootstrap · Backend startup and middleware wiring · `server/src/index.js`
- [HTTP routes](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/routes): api routes · Auth and CLI API boundaries · `server/src/routes`
- [Auth routes](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/routes/authRoutes.js): route module · Login, session, and device-flow endpoints · `server/src/routes/authRoutes.js`
- [CLI routes](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/routes/cliRoutes.js): route module · Endpoints used by the terminal client · `server/src/routes/cliRoutes.js`
- [Controllers](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/controllers): request handlers · Transport-to-service adapters · `server/src/controllers`
- [Auth core](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/auth.js): identity logic · Tokens, credentials, and auth middleware support · `server/src/lib/auth.js`
- [Credential store](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/credentialStore.js): secure storage · Persists sensitive user secrets outside plain files · `server/src/lib/credentialStore.js`
- [Token logic](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/lib/token.js): session/token · Issues and validates bearer/session tokens · `server/src/lib/token.js`
- [Middleware](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/middleware): express middleware · Auth enforcement and error normalization · `server/src/middleware`
- [CLI runtime](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/cli/main.js): terminal app · Commander-style command entry and execution layer · `server/src/cli/main.js`
- [API client](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/cli/utils/apiClient.js): cli transport · CLI bridge to backend HTTP APIs · `server/src/cli/utils/apiClient.js`
- [CLI commands](https://github.com/vishal34sd/smart-cli-based-agent/tree/main/server/src/cli/commands): command set · Auth, config, AI, and general action commands · `server/src/cli/commands`
- [AI services](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/services/aiService.js): model orchestration · Chat, streaming, and tool-calling orchestration · `server/src/services/aiService.js`
- [Tool config](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/config/toolConfig.js): agent config · Tool definitions and agent behavior settings · `server/src/config/toolConfig.js`
- [Database layer](https://github.com/vishal34sd/smart-cli-based-agent/blob/main/server/src/db/prisma.js): persistence · Prisma/Postgres access boundary and health checks · `server/src/db/prisma.js`

## Connections

- Auth screens → Login form: renders · Sign-in flow uses shared form UI
- Auth screens → Auth client: calls · Browser flow talks to backend auth APIs
- Auth client → API config: uses · Resolves backend endpoint configuration
- Next.js app → Auth screens: contains · Route segments for login and approval states
- Next.js app → Landing page: contains · Public landing entry
- Server entry → HTTP routes: mounts · Wires API routers into Express
- Server entry → Middleware: applies · Global auth and error handling pipeline
- HTTP routes → Auth routes: includes · Authentication/device-flow API
- HTTP routes → CLI routes: includes · CLI-facing API
- Auth routes → Controllers: delegates · Route handlers forward to controllers
- CLI routes → Controllers: delegates · CLI endpoints forward to controllers
- Controllers → Auth core: uses · Controllers rely on auth/session primitives
- Auth core → Token logic: depends on · Token issuance and validation
- Auth core → Credential store: persists via · Sensitive values stored securely
- Auth core → Database layer: reads/writes · Auth and device-flow state in database
- CLI runtime → API client: calls · Commands invoke backend HTTP APIs
- CLI runtime → CLI commands: loads · Main entry dispatches command modules
- CLI commands → AI services: invokes · AI commands delegate to model orchestration
- AI services → Tool config: uses · Tool-calling and agent-mode configuration
- AI services → Database layer: persists via · Conversation and message state stored in DB
- AI services → Middleware: protected by · Authenticated API access enforced at boundary
- Auth screens → Auth routes: approves via · Browser approval flow hits backend auth endpoints
- CLI commands → CLI routes: requests · Terminal actions use CLI API endpoints
