# derailed-dash/rickbot-adk architecture

> Architecture diagram of the GitHub repository derailed-dash/rickbot-adk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/derailed-dash/rickbot-adk
- Repository: https://github.com/derailed-dash/rickbot-adk

## Overview

1. **Repo shape: a multi-surface agent application**
   - This repository is a monorepo for a “Rickbot” multi-personality chatbot.
   - The runtime surfaces are split into a Python backend, a Next.js frontend, and a Streamlit frontend, with Docker and Cloud Run-style deployment support around them.

2. **Core backend is the central system boundary**
   - `src/main.py` is the backend entrypoint, exposing the API layer that the UIs talk to.
   - The backend is the main orchestration boundary: it hosts the chat API, persona discovery, auth checks, rate limiting, and agent execution.

3. **Agent layer: ADK-based persona chatbot**
   - `src/rickbot_agent/` contains the agent domain logic:
     - `agent.py` for the core agent setup
     - `personality.py` for persona selection and behavior
     - `services.py` and `tools_custom.py` for tool/service integration
     - `auth.py`, `auth_models.py`, and `auth_middleware.py` for access control concerns
   - The personas are data-driven from `src/rickbot_agent/data/personalities.yaml` and persona-specific system prompts in `src/rickbot_agent/data/system_prompts/`.

4. **Frontend split: modern Next.js UI and older Streamlit UI**
   - `src/nextjs_fe/` is the primary modern chat UI.
   - `src/streamlit_fe/` is a separate Streamlit interface, likely retained for lightweight access, demos, or internal workflows.
   - This means the repo supports two independent presentation layers over the same backend/agent domain.

5. **Next.js frontend talks to backend for personas and chat streaming**
   - The README says the Next.js UI fetches persona metadata from the backend `/personas` endpoint.
   - Chat uses streaming responses over SSE for incremental agent output.
   - The UI also supports file uploads and tool-status visibility, so the frontend is not just a simple message form; it renders agent execution state.

6. **Authentication and authorization are first-class**
   - The presence of `pages/api/auth/[...nextauth].ts`, `auth_middleware.py`, and multiple auth-related tests indicates a split auth model:
     - frontend session/auth handling in Next.js
     - backend authorization enforcement in Python
   - `src/tests/integration/test_rbac_e2e.py` suggests role/permission behavior is part of the runtime architecture, not just an add-on.

7. **Shared utilities centralize cross-cutting concerns**
   - `src/rickbot_utils/` contains reusable infrastructure-like code:
     - configuration
     - logging
     - rate limiting
     - secret handling
   - This package sits between the app layer and deployment/environment concerns, so it likely provides the common plumbing used by both backend and tests.

8. **External services and cloud dependencies shape the design**
   - The repo is clearly built around Google Cloud and Gemini/ADK:
     - Gemini Developer API / Google GenAI SDK
     - Google Cloud deployment via Terraform
     - Firestore for persistence
     - Cloud Storage for object/file artifacts
     - Secret Manager for sensitive config
     - Artifact Registry and build triggers for CI/CD
   - The architecture therefore has a cloud-backed control plane for secrets/builds and a runtime data plane for chat/state/artifacts.

9. **RAG/file-search capability is an explicit subsystem**
   - The README and notebooks indicate a File Search Store used for retrieval-augmented responses, especially for certain personas.
   - `notebooks/file_search_store.ipynb` and related integration tests show that this is treated as a managed external capability, not embedded app state.

10. **Deployment is container-first and environment-driven**
   - Multiple Dockerfiles exist:
     - `Dockerfile.api`
     - `Dockerfile.streamlit`
     - `Dockerfile.unified`
   - `docker-compose.yml` and `Makefile` support local orchestration, while `deployment/terraform/` defines the cloud infrastructure.
   - The existence of separate API, Streamlit, frontend, and unified images suggests deployment flexibility across isolated and combined runtime topologies.

11. **CI/CD and release automation are part of the architecture**
   - `.cloudbuild/` and `.github/workflows/` show two automation paths:
     - Cloud Build pipelines for staging/prod-style deployment
     - GitHub Actions for dispatch/review/triage and related automation
   - The repo is not just source code; it includes operational automation for validation and promotion.

12. **Testing is layered across unit, integration, and UI**
   - `src/tests/unit`, `src/tests/integration`, and the Next.js test suite under `src/nextjs_fe/__tests__` indicate clear separation:
     - unit tests for local logic
     - integration tests for cloud/service behavior
     - frontend tests for UI state and interaction
   - This mirrors the app boundaries and reinforces where the important seams are.

13. **Auxiliary content supports operations and evolution**
   - `conductor/` contains archived design/spec/plan artifacts and workflow notes, which look like project management or agent-guided development records rather than runtime code.
   - `docs/` and `deployment/README.md` carry the architectural rationale and environment-specific setup, so the repo’s “real architecture docs” are intentionally split from code.

14. **Main end-to-end flow**
   - User interacts through either the Next.js UI or Streamlit UI.
   - The UI authenticates the user and sends chat requests to the Python backend.
   - The backend validates access, selects the appropriate persona, invokes the ADK-powered agent, optionally consults tools/RAG/file search, and streams the response back to the client.
   - Deployment and secrets are managed separately through Cloud Build, Terraform, and Google Cloud services.

Main entry points: [`src/main.py`](https://github.com/derailed-dash/rickbot-adk/blob/main/src/main.py) (Backend API), [`src/rickbot_agent/agent.py`](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/agent.py) (Agent Core), [`src/nextjs_fe/pages/index.tsx`](https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/pages/index.tsx) (Next.js UI), [`src/rickbot_agent/auth.py`](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth.py) (Access Control)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_api["Backend API<br/>fastapi entrypoint<br/>[main.py]"]
  node_agent["Agent Core<br/>adk agent<br/>[agent.py]"]
  node_persona["Persona Router<br/>persona logic<br/>[personality.py]"]
  node_persona_data["Persona Data<br/>persona assets<br/>[personalities.yaml]"]
  node_auth["Access Control<br/>rbac auth<br/>[auth.py]"]
  node_auth_models["Auth Models<br/>policy models<br/>[auth_models.py]"]
  node_auth_mw["Auth Middleware<br/>request middleware<br/>[auth_middleware.py]"]
  node_tools["Tools & Services<br/>tool adapters<br/>[tools_custom.py]"]
  node_services["Services<br/>service layer<br/>[services.py]"]
  node_shared["Shared Utils<br/>common plumbing<br/>[config.py]"]
end

subgraph group_frontends["Frontends"]
  node_nextui["Next.js UI<br/>primary frontend<br/>[index.tsx]"]
  node_nextauth["NextAuth API<br/>session auth<br/>[[...nextauth].ts]"]
  node_streamlit["Streamlit UI<br/>secondary frontend<br/>[app.py]"]
end

subgraph group_platform["Platform"]
  node_deploy["Terraform<br/>infra as code<br/>[apis.tf]"]
  node_cloudbuild["Cloud Build<br/>ci/cd<br/>[staging.yaml]"]
  node_githubwf["GitHub Actions<br/>automation<br/>[gemini-review.yml]"]
  node_dockerfiles["Container Images<br/>runtime packaging<br/>[Dockerfile.unified]"]
  node_compose["Local Compose<br/>local orchestration<br/>[docker-compose.yml]"]
end

subgraph group_quality["Quality"]
  node_tests_api["API Tests<br/>integration tests<br/>[test_api.py]"]
  node_tests_agent["Agent Tests<br/>integration tests"]
  node_tests_ui["UI Tests<br/>frontend tests<br/>[Chat.test.tsx]"]
end

node_nextui -->|"auth session"| node_nextauth
node_nextui -->|"chat + personas"| node_api
node_streamlit -->|"chat API"| node_api
node_api -->|"invoke"| node_agent
node_api -->|"select persona"| node_persona
node_api -->|"authorize"| node_auth
node_api -->|"tool calls"| node_tools
node_api -->|"plumbing"| node_shared
node_agent -->|"prompting"| node_persona
node_agent -->|"extensions"| node_tools
node_persona -.->|"driven by"| node_persona_data
node_auth -->|"models"| node_auth_models
node_auth -->|"middleware"| node_auth_mw
node_deploy -->|"build"| node_dockerfiles
node_deploy -->|"release"| node_cloudbuild
node_cloudbuild -->|"apply"| node_deploy
node_compose -->|"local run"| node_api
node_compose -->|"local run"| node_nextui
node_githubwf -.->|"validate"| node_tests_api
node_githubwf -.->|"validate"| node_tests_ui
node_tests_api -.->|"covers"| node_api
node_tests_agent -.->|"covers"| node_agent
node_tests_ui -.->|"covers"| node_nextui

click node_api "https://github.com/derailed-dash/rickbot-adk/blob/main/src/main.py"
click node_agent "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/agent.py"
click node_persona "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/personality.py"
click node_persona_data "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/data/personalities.yaml"
click node_auth "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth.py"
click node_auth_models "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth_models.py"
click node_auth_mw "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth_middleware.py"
click node_tools "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/tools_custom.py"
click node_services "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/services.py"
click node_shared "https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_utils/config.py"
click node_nextui "https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/pages/index.tsx"
click node_nextauth "https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/pages/api/auth/[...nextauth].ts"
click node_streamlit "https://github.com/derailed-dash/rickbot-adk/blob/main/src/streamlit_fe/app.py"
click node_deploy "https://github.com/derailed-dash/rickbot-adk/blob/main/deployment/terraform/apis.tf"
click node_cloudbuild "https://github.com/derailed-dash/rickbot-adk/blob/main/.cloudbuild/staging.yaml"
click node_githubwf "https://github.com/derailed-dash/rickbot-adk/blob/main/.github/workflows/gemini-review.yml"
click node_dockerfiles "https://github.com/derailed-dash/rickbot-adk/blob/main/Dockerfile.unified"
click node_compose "https://github.com/derailed-dash/rickbot-adk/blob/main/docker-compose.yml"
click node_tests_api "https://github.com/derailed-dash/rickbot-adk/blob/main/src/tests/integration/test_api.py"
click node_tests_agent "https://github.com/derailed-dash/rickbot-adk/blob/main/src/tests/integration/test_rickbot_agent_multiturn.py"
click node_tests_ui "https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/__tests__/Chat.test.tsx"

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_api,node_agent,node_persona,node_persona_data,node_auth,node_auth_models,node_auth_mw,node_tools,node_services,node_shared toneBlue
class node_nextui,node_nextauth,node_streamlit toneAmber
class node_deploy,node_cloudbuild,node_githubwf,node_dockerfiles,node_compose toneMint
class node_tests_api,node_tests_agent,node_tests_ui toneRose
```

## Components

### Runtime

Python backend, agent domain, and shared runtime plumbing

- [Backend API](https://github.com/derailed-dash/rickbot-adk/blob/main/src/main.py): fastapi entrypoint · Main Python entrypoint exposing chat, personas, auth, streaming, and orchestration · `src/main.py`
- [Agent Core](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/agent.py): adk agent · Primary agent setup and execution boundary · `src/rickbot_agent/agent.py`
- [Persona Router](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/personality.py): persona logic · Selects and shapes persona behavior for each conversation · `src/rickbot_agent/personality.py`
- [Persona Data](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/data/personalities.yaml): persona assets · YAML persona definitions and persona-specific system prompts · `src/rickbot_agent/data/personalities.yaml`
- [Access Control](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth.py): rbac auth · Authentication, authorization, and request enforcement · `src/rickbot_agent/auth.py`
- [Auth Models](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth_models.py): policy models · Shared auth and role model definitions · `src/rickbot_agent/auth_models.py`
- [Auth Middleware](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/auth_middleware.py): request middleware · Backend request interception and auth enforcement layer · `src/rickbot_agent/auth_middleware.py`
- [Tools & Services](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/tools_custom.py): tool adapters · Custom tools and service integrations used by the agent · `src/rickbot_agent/tools_custom.py`
- [Services](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_agent/services.py): service layer · Agent-facing service orchestration helpers · `src/rickbot_agent/services.py`
- [Shared Utils](https://github.com/derailed-dash/rickbot-adk/blob/main/src/rickbot_utils/config.py): common plumbing · Configuration, logging, rate limiting, and secret helpers · `src/rickbot_utils/config.py`

### Frontends

User-facing chat surfaces

- [Next.js UI](https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/pages/index.tsx): primary frontend · Modern web chat surface with auth, personas, uploads, and streaming · `src/nextjs_fe/pages/index.tsx`
- [NextAuth API](https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/pages/api/auth/[...nextauth].ts): session auth · Frontend session and auth callback route · `src/nextjs_fe/pages/api/auth/[...nextauth].ts`
- [Streamlit UI](https://github.com/derailed-dash/rickbot-adk/blob/main/src/streamlit_fe/app.py): secondary frontend · Alternative Streamlit interface for lightweight access and workflows · `src/streamlit_fe/app.py`

### Platform

Containers, cloud infra, and automation

- [Terraform](https://github.com/derailed-dash/rickbot-adk/blob/main/deployment/terraform/apis.tf): infra as code · Google Cloud infrastructure definitions for runtime, storage, secrets, and IAM · `deployment/terraform/apis.tf`
- [Cloud Build](https://github.com/derailed-dash/rickbot-adk/blob/main/.cloudbuild/staging.yaml): ci/cd · Staging and production deployment pipelines · `.cloudbuild/staging.yaml`
- [GitHub Actions](https://github.com/derailed-dash/rickbot-adk/blob/main/.github/workflows/gemini-review.yml): automation · Repository automation for review, triage, and dispatch workflows · `.github/workflows/gemini-review.yml`
- [Container Images](https://github.com/derailed-dash/rickbot-adk/blob/main/Dockerfile.unified): runtime packaging · API, Streamlit, and unified Docker build targets · `Dockerfile.unified`
- [Local Compose](https://github.com/derailed-dash/rickbot-adk/blob/main/docker-compose.yml): local orchestration · Local multi-service composition and developer startup support · `docker-compose.yml`

### Quality

Testing and verification

- [API Tests](https://github.com/derailed-dash/rickbot-adk/blob/main/src/tests/integration/test_api.py): integration tests · Backend API, auth, RBAC, rate limiting, and streaming coverage · `src/tests/integration/test_api.py`
- [Agent Tests](https://github.com/derailed-dash/rickbot-adk/blob/main/src/tests/integration/test_rickbot_agent_multiturn.py): integration tests · Multi-turn agent, personas, retrieval, and tool-status coverage · `src/tests/integration/test_rickbot_agent_multiturn.py`
- [UI Tests](https://github.com/derailed-dash/rickbot-adk/blob/main/src/nextjs_fe/__tests__/Chat.test.tsx): frontend tests · Next.js component and interaction coverage · `src/nextjs_fe/__tests__/Chat.test.tsx`

## Connections

- Next.js UI → NextAuth API: auth session · Next.js UI relies on the auth route for session handling
- Next.js UI → Backend API: chat + personas · Fetches persona metadata and sends chat requests to the backend
- Streamlit UI → Backend API: chat API · Alternative presentation layer over the same backend
- Backend API → Agent Core: invoke · Backend orchestrates agent execution
- Backend API → Persona Router: select persona · Resolves persona metadata and behavior per request
- Backend API → Access Control: authorize · Enforces authentication and RBAC before execution
- Backend API → Tools & Services: tool calls · Delegates external actions and custom integrations
- Backend API → Shared Utils: plumbing · Uses shared config, logging, rate limits, and secrets helpers
- Agent Core → Persona Router: prompting · Uses persona selection and behavior rules
- Agent Core → Tools & Services: extensions · Calls services and custom tools during execution
- Persona Router → Persona Data: driven by · Persona logic is backed by YAML definitions and prompt files
- Access Control → Auth Models: models · Authorization logic uses shared auth model definitions
- Access Control → Auth Middleware: middleware · Request enforcement is applied through middleware
- Terraform → Container Images: build · Infrastructure is deployed from container images
- Terraform → Cloud Build: release · Cloud Build coordinates deployment flows
- Cloud Build → Terraform: apply · Pipelines promote configured infrastructure and services
- Local Compose → Backend API: local run · Compose supports local backend orchestration
- Local Compose → Next.js UI: local run · Compose supports local frontend orchestration
- GitHub Actions → API Tests: validate · Automation gates changes with backend checks
- GitHub Actions → UI Tests: validate · Automation gates changes with frontend checks
- API Tests → Backend API: covers · Integration tests exercise backend endpoints and runtime behavior
- Agent Tests → Agent Core: covers · Integration tests exercise the agent domain and retrieval paths
- UI Tests → Next.js UI: covers · Frontend tests exercise chat, auth, and UI state
