How vishal34sd/smart-cli-based-agent works
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: auth.js Auth core · aiService.js AI services · page.tsx Auth screens · routes HTTP routes
Components 21
Client
Next.js app for sign-in and device approval
- Next.js app · App Router frontend for login and approval flows
client/app - Auth screens · Sign-in, approve, and device pages
client/app/(auth)/sign-in/page.tsx - Landing page · Top-level client entry page
client/app/page.tsx - Login form · Reusable sign-in UI
client/components/LoginForm.tsx - Auth client · Frontend helper for backend auth requests
client/lib/auth-client.ts - API config · Backend base URL and client API settings
client/config/api.ts
Server
Express API, CLI runtime, AI, and persistence
- Server entry · Backend startup and middleware wiring
server/src/index.js - HTTP routes · Auth and CLI API boundaries
server/src/routes - Auth routes · Login, session, and device-flow endpoints
server/src/routes/authRoutes.js - CLI routes · Endpoints used by the terminal client
server/src/routes/cliRoutes.js - Controllers · Transport-to-service adapters
server/src/controllers - Auth core · Tokens, credentials, and auth middleware support
server/src/lib/auth.js - Credential store · Persists sensitive user secrets outside plain files
server/src/lib/credentialStore.js - Token logic · Issues and validates bearer/session tokens
server/src/lib/token.js - Middleware · Auth enforcement and error normalization
server/src/middleware - CLI runtime · Commander-style command entry and execution layer
server/src/cli/main.js - API client · CLI bridge to backend HTTP APIs
server/src/cli/utils/apiClient.js - CLI commands · Auth, config, AI, and general action commands
server/src/cli/commands - AI services · Chat, streaming, and tool-calling orchestration
server/src/services/aiService.js - Tool config · Tool definitions and agent behavior settings
server/src/config/toolConfig.js - Database layer · Prisma/Postgres access boundary and health checks
server/src/db/prisma.js
Connections 23
- 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