# muideen-ha-shem/get-kc architecture

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

- Interactive diagram: https://gitdiagram.com/muideen-ha-shem/get-kc
- Repository: https://github.com/muideen-ha-shem/get-kc

## Overview

#### Purpose and Runtime Entry Points
HavisIQ is a multi-tenant AI business-solutions advisor: it answers customer questions with cited RAG evidence, recommends products/services, and supports account, support, knowledge-management, and administration workflows. The primary API runtime is `src/api/app.py`; chat coordination centers on `src/orchestrator/chat_orchestrator.py`. The React/Vite client starts at `frontend/src/main.tsx` and composes routes/UI in `frontend/src/App.tsx`. A separate CLI path exists in `src/chat_cli.py`; MCP tooling is independently exposed from `src/mcp/server.py`.

#### API Boundary and Application Services
FastAPI routes form the HTTP boundary, validate schemas, resolve auth/workspace context, and delegate to services rather than embedding business logic. Chat, auth, workspace, knowledge, support, agent, and admin route groups live under `src/api/routes/chat.py`, `src/api/routes/auth.py`, and `src/api/routes/knowledge_management.py`. Shared request dependencies—including optional versus required identity—are centralized in `src/api/deps.py`.

#### Chat Orchestration and Grounded Answer Pipeline
`ChatOrchestrator` is the principal control-flow coordinator: it resolves session references, routes a question, assembles evidence, runs advisory behavior, generates an answer, validates citations, and returns a response. Core stages are represented by `src/orchestrator/chat_orchestrator.py`, `src/services/session/session_service.py`, and `src/services/advisory/advisory_layer.py`. The baseline RAG adapters for embeddings, vector retrieval, and LLM generation are `src/api/services/embeddings.py`, `src/api/services/retrieval.py`, and `src/api/services/generator.py`.

#### Multi-Source Retrieval Boundary
Retrieval can select internal knowledge, live web sources, or both. Routing and query preparation precede provider calls; fetched evidence is filtered/reranked/ranked, merged, and citation-cleaned before generation. This pipeline is grounded in `src/services/routing/source_router.py`, `src/services/manager/search_manager.py`, and `src/services/merger/context_merger.py`. Search providers are pluggable in `src/services/search/providers/tavily.py` and `src/services/search/providers/brave.py`; absent provider keys degrade to knowledge-base-only behavior. In-memory process-lifetime caches are a performance boundary rather than persistent state.

#### Tenant, Identity, and Persistence Model
Supabase supplies PostgreSQL/pgvector, Supabase Auth, and row-level-security-backed tenant/customer data. The infrastructure adapter is `src/infrastructure/database/supabase.py`; workspace isolation and resolution are defined in `src/services/workspace/workspace_resolver.py` and `src/services/workspace/workspace_context.py`. Authentication wraps Supabase tokens in `src/services/auth/auth_service.py`. Customer conversation persistence is deliberately best-effort behind chat and managed through `src/services/conversation/conversation_service.py`. Schema evolution, including workspaces, identity, escalation, and knowledge, is captured in `scripts/sql/004_customer_identity.sql`, `scripts/sql/007_workspace_foundation.sql`, and `scripts/sql/012_knowledge_management.sql`.

#### Operational Workflows Beyond Chat
Knowledge management manages collections, sources, uploads, crawling, ingestion, versioning, preview, quality, and scheduling through `src/services/knowledge_management/ingestion_service.py`, `src/services/knowledge_management/crawl_service.py`, and `src/services/knowledge_management/collection_service.py`. Human-support escalation selects/routes agents and creates operational context via `src/services/escalation/escalation_service.py`, `src/services/escalation/routing.py`, and `src/services/agents/agent_service.py`. Appointments, demo requests, feedback, saved items, and notifications are separate transactional workflows, with notification delivery deliberately centralized in `src/services/notifications/notification_service.py`.

#### Frontend and Embeddable Client Surfaces
The frontend is a React/TypeScript customer and operations portal. Its shared API/auth boundary is `frontend/src/lib/apiClient.ts` and `frontend/src/lib/authContext.tsx`; customer dashboard and knowledge UI are `frontend/src/pages/DashboardPage.tsx` and `frontend/src/pages/knowledge/KnowledgeManagementPage.tsx`; platform administration is rooted at `frontend/src/pages/admin/AdminLayout.tsx`. An independently buildable embedding/widget SDK exposes a client/widget surface from `sdk/src/index.ts`, `sdk/src/client.ts`, and `sdk/src/widget.ts`.

#### External Services and Offline Ingestion
Production dependencies are Groq for answer generation, Google Gemini for embeddings/reranking, Supabase for auth/database/vector search, and optionally Tavily or Brave for web search. Offline knowledge acquisition and vector loading are intentionally outside the API runtime: `scripts/crawl.py`, `scripts/chunk_runner.py`, and `scripts/upload_vectors.py`. Product-specific crawler scripts indicate knowledge is ingested per solution; deployment must apply schema migrations and execute ingestion before product-scoped retrieval data is available.

Main entry points: [`src/orchestrator/chat_orchestrator.py`](https://github.com/muideen-ha-shem/get-kc/blob/main/src/orchestrator/chat_orchestrator.py) (Chat orchestrator), [`src/infrastructure/database/supabase.py`](https://github.com/muideen-ha-shem/get-kc/blob/main/src/infrastructure/database/supabase.py) (Supabase), [`src/api/routes/chat.py`](https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/routes/chat.py) (Routes & identity deps), [`src/services/manager/search_manager.py`](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/manager/search_manager.py) (Search manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client Surfaces"]
  node_portal["React portal<br/>React/Vite UI<br/>[App.tsx]"]
  node_portal_api_auth["Portal API &amp; auth<br/>client boundary<br/>[apiClient.ts]"]
  node_widget_sdk["Embed SDK<br/>widget client<br/>[index.ts]"]
  node_cli["Chat CLI<br/>CLI entry point<br/>[chat_cli.py]"]
end

subgraph group_api["API Runtime"]
  node_fastapi["FastAPI application<br/>HTTP entry point<br/>[app.py]"]
  node_routes_deps["Routes &amp; identity deps<br/>HTTP boundary<br/>[chat.py]"]
  node_auth["Authentication service<br/>auth adapter<br/>[auth_service.py]"]
  node_mcp["MCP server<br/>tooling entry point<br/>[server.py]"]
end

subgraph group_chat["Grounded Chat"]
  node_chat_orchestrator{{"Chat orchestrator<br/>control flow"}}
  node_sessions["Session service<br/>session management<br/>[session_service.py]"]
  node_advisory["Advisory layer<br/>recommendations<br/>[advisory_layer.py]"]
  node_source_router["Source router<br/>retrieval routing<br/>[source_router.py]"]
  node_search_manager["Search manager<br/>evidence pipeline<br/>[search_manager.py]"]
  node_context_merger["Context merger<br/>evidence assembly<br/>[context_merger.py]"]
  node_generation["RAG generation adapters<br/>embeddings, retrieval, LLM<br/>[generator.py]"]
end

subgraph group_data["Tenant Data &amp; Retrieval"]
  node_workspace["Workspace resolver<br/>tenant context"]
  node_conversations["Conversation service<br/>best-effort persistence"]
  node_supabase[("Supabase<br/>auth, PostgreSQL, pgvector<br/>[supabase.py]")]
  node_web_providers["Web search providers<br/>Tavily / Brave<br/>[tavily.py]"]
end

subgraph group_operations["Operations &amp; Ingestion"]
  node_knowledge_management["Knowledge management<br/>ingestion lifecycle"]
  node_escalation["Support escalation<br/>human handoff"]
  node_notifications["Notification service<br/>delivery workflow"]
  node_offline_ingestion["Offline vector ingestion<br/>crawl, chunk, upload<br/>[upload_vectors.py]"]
end

node_portal -->|"uses"| node_portal_api_auth
node_portal_api_auth -->|"HTTP API"| node_fastapi
node_widget_sdk -->|"embedded API calls"| node_fastapi
node_cli -->|"chat requests"| node_chat_orchestrator
node_mcp -->|"tool requests"| node_chat_orchestrator
node_fastapi -->|"dispatches"| node_routes_deps
node_routes_deps -->|"authenticates"| node_auth
node_routes_deps -->|"resolves tenant"| node_workspace
node_routes_deps -->|"chat requests"| node_chat_orchestrator
node_routes_deps -->|"management requests"| node_knowledge_management
node_routes_deps -->|"support requests"| node_escalation
node_auth -->|"token validation"| node_supabase
node_workspace -->|"RLS tenant data"| node_supabase
node_chat_orchestrator -->|"resolves session"| node_sessions
node_chat_orchestrator -->|"routes question"| node_source_router
node_chat_orchestrator -->|"applies advisory logic"| node_advisory
node_chat_orchestrator -->|"assembles evidence"| node_context_merger
node_chat_orchestrator -->|"generates and validates"| node_generation
node_chat_orchestrator -.->|"persists best effort"| node_conversations
node_source_router -->|"prepares selected sources"| node_search_manager
node_search_manager -->|"internal vector retrieval"| node_supabase
node_search_manager -.->|"live web retrieval"| node_web_providers
node_search_manager -->|"ranked evidence"| node_context_merger
node_context_merger -->|"grounded context"| node_generation
node_generation -->|"vector retrieval"| node_supabase
node_conversations -.->|"stores conversations"| node_supabase
node_knowledge_management -->|"manages knowledge"| node_supabase
node_offline_ingestion -->|"loads vectors"| node_supabase
node_escalation -->|"notifies operations"| node_notifications
node_escalation -->|"stores handoff context"| node_supabase

click node_portal "https://github.com/muideen-ha-shem/get-kc/blob/main/frontend/src/App.tsx"
click node_portal_api_auth "https://github.com/muideen-ha-shem/get-kc/blob/main/frontend/src/lib/apiClient.ts"
click node_widget_sdk "https://github.com/muideen-ha-shem/get-kc/blob/main/sdk/src/index.ts"
click node_fastapi "https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/app.py"
click node_routes_deps "https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/routes/chat.py"
click node_auth "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/auth/auth_service.py"
click node_chat_orchestrator "https://github.com/muideen-ha-shem/get-kc/blob/main/src/orchestrator/chat_orchestrator.py"
click node_sessions "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/session/session_service.py"
click node_advisory "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/advisory/advisory_layer.py"
click node_source_router "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/routing/source_router.py"
click node_search_manager "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/manager/search_manager.py"
click node_context_merger "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/merger/context_merger.py"
click node_generation "https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/services/generator.py"
click node_workspace "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/workspace/workspace_resolver.py"
click node_conversations "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/conversation/conversation_service.py"
click node_supabase "https://github.com/muideen-ha-shem/get-kc/blob/main/src/infrastructure/database/supabase.py"
click node_web_providers "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/search/providers/tavily.py"
click node_knowledge_management "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/knowledge_management/ingestion_service.py"
click node_escalation "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/escalation/escalation_service.py"
click node_notifications "https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/notifications/notification_service.py"
click node_offline_ingestion "https://github.com/muideen-ha-shem/get-kc/blob/main/scripts/upload_vectors.py"
click node_cli "https://github.com/muideen-ha-shem/get-kc/blob/main/src/chat_cli.py"
click node_mcp "https://github.com/muideen-ha-shem/get-kc/blob/main/src/mcp/server.py"

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_portal,node_portal_api_auth,node_widget_sdk,node_cli toneBlue
class node_fastapi,node_routes_deps,node_auth,node_mcp toneAmber
class node_chat_orchestrator,node_sessions,node_advisory,node_source_router,node_search_manager,node_context_merger,node_generation toneMint
class node_workspace,node_conversations,node_supabase,node_web_providers toneRose
class node_knowledge_management,node_escalation,node_notifications,node_offline_ingestion toneIndigo
```

## Components

### Client Surfaces

- [React portal](https://github.com/muideen-ha-shem/get-kc/blob/main/frontend/src/App.tsx): React/Vite UI · Customer, knowledge, and administration workflows. · `frontend/src/App.tsx`
- [Portal API & auth](https://github.com/muideen-ha-shem/get-kc/blob/main/frontend/src/lib/apiClient.ts): client boundary · `frontend/src/lib/apiClient.ts`
- [Embed SDK](https://github.com/muideen-ha-shem/get-kc/blob/main/sdk/src/index.ts): widget client · `sdk/src/index.ts`
- [Chat CLI](https://github.com/muideen-ha-shem/get-kc/blob/main/src/chat_cli.py): CLI entry point · `src/chat_cli.py`

### API Runtime

- [FastAPI application](https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/app.py): HTTP entry point · `src/api/app.py`
- [Routes & identity deps](https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/routes/chat.py): HTTP boundary · Validates requests and resolves auth/workspace context. · `src/api/routes/chat.py`
- [Authentication service](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/auth/auth_service.py): auth adapter · `src/services/auth/auth_service.py`
- [MCP server](https://github.com/muideen-ha-shem/get-kc/blob/main/src/mcp/server.py): tooling entry point · `src/mcp/server.py`

### Grounded Chat

- [Chat orchestrator](https://github.com/muideen-ha-shem/get-kc/blob/main/src/orchestrator/chat_orchestrator.py): control flow · Coordinates routing, evidence, advisory behavior, answers, and citations. · `src/orchestrator/chat_orchestrator.py`
- [Session service](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/session/session_service.py): session management · `src/services/session/session_service.py`
- [Advisory layer](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/advisory/advisory_layer.py): recommendations · `src/services/advisory/advisory_layer.py`
- [Source router](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/routing/source_router.py): retrieval routing · Selects internal knowledge, web, or both. · `src/services/routing/source_router.py`
- [Search manager](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/manager/search_manager.py): evidence pipeline · Fetches, filters, reranks, and ranks evidence. · `src/services/manager/search_manager.py`
- [Context merger](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/merger/context_merger.py): evidence assembly · Merges and citation-cleans ranked context. · `src/services/merger/context_merger.py`
- [RAG generation adapters](https://github.com/muideen-ha-shem/get-kc/blob/main/src/api/services/generator.py): embeddings, retrieval, LLM · `src/api/services/generator.py`

### Tenant Data & Retrieval

- [Workspace resolver](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/workspace/workspace_resolver.py): tenant context · Establishes workspace isolation for requests. · `src/services/workspace/workspace_resolver.py`
- [Conversation service](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/conversation/conversation_service.py): best-effort persistence · `src/services/conversation/conversation_service.py`
- [Supabase](https://github.com/muideen-ha-shem/get-kc/blob/main/src/infrastructure/database/supabase.py): auth, PostgreSQL, pgvector · RLS-backed tenant data and vector search. · `src/infrastructure/database/supabase.py`
- [Web search providers](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/search/providers/tavily.py): Tavily / Brave · Optional providers; missing keys fall back to knowledge-base retrieval. · `src/services/search/providers/tavily.py`

### Operations & Ingestion

- [Knowledge management](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/knowledge_management/ingestion_service.py): ingestion lifecycle · Manages collections, sources, uploads, versions, and quality. · `src/services/knowledge_management/ingestion_service.py`
- [Support escalation](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/escalation/escalation_service.py): human handoff · Routes agents and creates operational context. · `src/services/escalation/escalation_service.py`
- [Notification service](https://github.com/muideen-ha-shem/get-kc/blob/main/src/services/notifications/notification_service.py): delivery workflow · `src/services/notifications/notification_service.py`
- [Offline vector ingestion](https://github.com/muideen-ha-shem/get-kc/blob/main/scripts/upload_vectors.py): crawl, chunk, upload · Must run after migrations before product retrieval is available. · `scripts/upload_vectors.py`

## Connections

- React portal → Portal API & auth: uses
- Portal API & auth → FastAPI application: HTTP API
- Embed SDK → FastAPI application: embedded API calls
- Chat CLI → Chat orchestrator: chat requests
- MCP server → Chat orchestrator: tool requests
- FastAPI application → Routes & identity deps: dispatches
- Routes & identity deps → Authentication service: authenticates
- Routes & identity deps → Workspace resolver: resolves tenant
- Routes & identity deps → Chat orchestrator: chat requests
- Routes & identity deps → Knowledge management: management requests
- Routes & identity deps → Support escalation: support requests
- Authentication service → Supabase: token validation
- Workspace resolver → Supabase: RLS tenant data
- Chat orchestrator → Session service: resolves session
- Chat orchestrator → Source router: routes question
- Chat orchestrator → Advisory layer: applies advisory logic
- Chat orchestrator → Context merger: assembles evidence
- Chat orchestrator → RAG generation adapters: generates and validates
- Chat orchestrator → Conversation service: persists best effort
- Source router → Search manager: prepares selected sources
- Search manager → Supabase: internal vector retrieval
- Search manager → Web search providers: live web retrieval
- Search manager → Context merger: ranked evidence
- Context merger → RAG generation adapters: grounded context
- RAG generation adapters → Supabase: vector retrieval
- Conversation service → Supabase: stores conversations
- Knowledge management → Supabase: manages knowledge
- Offline vector ingestion → Supabase: loads vectors
- Support escalation → Notification service: notifies operations
- Support escalation → Supabase: stores handoff context
