# k-abhishek/ai-matrix-backend architecture

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

- Interactive diagram: https://gitdiagram.com/k-abhishek/ai-matrix-backend
- Repository: https://github.com/k-abhishek/ai-matrix-backend

## Overview

AI Matrix is a multi-tenant backend combining website crawling and semantic search, conversational tool-using agents, connector/MCP execution, and Indian legal research. The main documented flow is a client request through the API into crawling and indexing, or into a grounded chat agent that retrieves knowledge and responds. The graph also preserves the connector and legal-research capabilities. Edges rely on the README and SOURCE INDEX references; unsampled implementation details are not inferred beyond those references.

Main entry points: [`src/services/crawl.service.ts`](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/crawl.service.ts) (Crawl orchestration), [`src/app.ts`](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/app.ts) (HTTP API gateway), [`src/modules/runtime/agent-runtime.service.ts`](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/runtime/agent-runtime.service.ts) (Agent runtime), [`src/services/qdrant.service.ts`](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/qdrant.service.ts) (Vector index)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and tenancy"]
  node_gateway["HTTP API gateway<br/>[app.ts]"]
  node_auth["Authentication<br/>[auth.ts]"]
end

subgraph group_crawl["Crawl and search"]
  node_crawler["Crawl orchestration<br/>[crawl.service.ts]"]
  node_sitemap["Sitemap discovery<br/>[sitemap.service.ts]"]
  node_fetch["Safe page fetching<br/>[fetcher.service.ts]"]
  node_extract["Content extraction"]
  node_chunk["Token-aware chunking"]
  node_embedding["Embedding generation"]
  node_vectors[("Vector index<br/>[qdrant.service.ts]")]
  node_search["Semantic search<br/>[search.service.ts]"]
end

subgraph group_agent["Agents and chat"]
  node_chat["Chat orchestration<br/>[chat.service.ts]"]
  node_runtime["Agent runtime"]
  node_models["Model selection"]
  node_context["Context window"]
  node_threads[("Chat persistence<br/>[chat.repository.ts]")]
end

subgraph group_connectors["Connectors and MCP"]
  node_connections["Workspace connections"]
  node_oauth["OAuth authorization<br/>[auth.manager.ts]"]
  node_execution["Tool execution"]
  node_mcp["MCP client"]
end

subgraph group_legal["Legal research"]
  node_legal_ingest["Legal ingestion"]
  node_legal_discovery["Legal source discovery"]
  node_legal_research["Legal research agent"]
  node_legal_retrieval["Legal retrieval"]
end

node_client(("Frontend clients"))

node_client -->|"sends requests"| node_gateway
node_gateway -->|"authenticates"| node_auth
node_gateway -.->|"routes crawl requests"| node_crawler
node_gateway -->|"routes chat requests"| node_chat
node_crawler -->|"discovers URLs"| node_sitemap
node_crawler -->|"fetches pages"| node_fetch
node_crawler -->|"extracts content"| node_extract
node_crawler -->|"chunks content"| node_chunk
node_crawler -->|"generates embeddings"| node_embedding
node_crawler -->|"stores vectors"| node_vectors
node_embedding -->|"provides vectors"| node_vectors
node_search -->|"queries vectors"| node_vectors
node_chat -->|"invokes agent"| node_runtime
node_runtime -->|"selects model"| node_models
node_runtime -->|"manages context"| node_context
node_runtime -->|"loads conversation"| node_threads
node_connections -->|"starts authorization"| node_oauth
node_oauth -->|"triggers discovery"| node_connections
node_execution -->|"dispatches MCP tools"| node_mcp
node_legal_discovery -->|"supplies discovered documents"| node_legal_ingest
node_legal_ingest -->|"embeds legal chunks"| node_embedding
node_legal_ingest -->|"indexes legal content"| node_vectors
node_legal_research -.->|"retrieves authorities"| node_legal_retrieval

click node_gateway "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/app.ts"
click node_auth "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/middleware/auth.ts"
click node_crawler "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/crawl.service.ts"
click node_sitemap "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/sitemap.service.ts"
click node_fetch "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/fetcher.service.ts"
click node_extract "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/content-extractor.service.ts"
click node_chunk "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/chunking.service.ts"
click node_embedding "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/embedding.service.ts"
click node_vectors "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/qdrant.service.ts"
click node_search "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/search/search.service.ts"
click node_chat "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/chat/chat.service.ts"
click node_runtime "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/runtime/agent-runtime.service.ts"
click node_models "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/modelFactory.service.ts"
click node_context "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/contextWindow.service.ts"
click node_threads "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/chat/chat.repository.ts"
click node_connections "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/connections/connection.service.ts"
click node_oauth "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/connections/auth.manager.ts"
click node_execution "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/execution/execution-engine.service.ts"
click node_mcp "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/mcp/mcp-client.manager.ts"
click node_legal_ingest "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/ingestion/legal-ingestion.service.ts"
click node_legal_discovery "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/discovery/legal-discovery.service.ts"
click node_legal_research "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/agents/legal-research.graph.ts"
click node_legal_retrieval "https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/retrieval/hybrid-search.service.ts"

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_gateway,node_auth,node_client toneBlue
class node_crawler,node_sitemap,node_fetch,node_extract,node_chunk,node_embedding,node_vectors,node_search toneAmber
class node_chat,node_runtime,node_models,node_context,node_threads toneMint
class node_connections,node_oauth,node_execution,node_mcp toneRose
class node_legal_ingest,node_legal_discovery,node_legal_research,node_legal_retrieval toneIndigo
```

## Components

### API and tenancy

- [HTTP API gateway](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/app.ts): `src/app.ts`
- [Authentication](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/middleware/auth.ts): `src/middleware/auth.ts`

### Crawl and search

- [Crawl orchestration](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/crawl.service.ts): `src/services/crawl.service.ts`
- [Sitemap discovery](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/sitemap.service.ts): `src/services/sitemap.service.ts`
- [Safe page fetching](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/fetcher.service.ts): `src/services/fetcher.service.ts`
- [Content extraction](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/content-extractor.service.ts): `src/services/content-extractor.service.ts`
- [Token-aware chunking](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/chunking.service.ts): `src/services/chunking.service.ts`
- [Embedding generation](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/embedding.service.ts): `src/services/embedding.service.ts`
- [Vector index](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/qdrant.service.ts): `src/services/qdrant.service.ts`
- [Semantic search](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/search/search.service.ts): `src/modules/search/search.service.ts`

### Agents and chat

- [Chat orchestration](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/chat/chat.service.ts): `src/modules/chat/chat.service.ts`
- [Agent runtime](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/runtime/agent-runtime.service.ts): `src/modules/runtime/agent-runtime.service.ts`
- [Model selection](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/modelFactory.service.ts): `src/services/modelFactory.service.ts`
- [Context window](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/services/contextWindow.service.ts): `src/services/contextWindow.service.ts`
- [Chat persistence](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/chat/chat.repository.ts): `src/modules/chat/chat.repository.ts`

### Connectors and MCP

- [Workspace connections](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/connections/connection.service.ts): `src/modules/connections/connection.service.ts`
- [OAuth authorization](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/connections/auth.manager.ts): `src/modules/connections/auth.manager.ts`
- [Tool execution](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/execution/execution-engine.service.ts): `src/modules/execution/execution-engine.service.ts`
- [MCP client](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/mcp/mcp-client.manager.ts): `src/modules/mcp/mcp-client.manager.ts`

### Legal research

- [Legal ingestion](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/ingestion/legal-ingestion.service.ts): `src/modules/legal-research/ingestion/legal-ingestion.service.ts`
- [Legal source discovery](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/discovery/legal-discovery.service.ts): `src/modules/legal-research/discovery/legal-discovery.service.ts`
- [Legal research agent](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/agents/legal-research.graph.ts): `src/modules/legal-research/agents/legal-research.graph.ts`
- [Legal retrieval](https://github.com/k-abhishek/ai-matrix-backend/blob/main/src/modules/legal-research/retrieval/hybrid-search.service.ts): `src/modules/legal-research/retrieval/hybrid-search.service.ts`

### Other components

- **Frontend clients**

## Connections

- Frontend clients → HTTP API gateway: sends requests (evidence: `README.md`)
- HTTP API gateway → Authentication: authenticates (evidence: `src/app.ts`)
- HTTP API gateway → Crawl orchestration: routes crawl requests
- HTTP API gateway → Chat orchestration: routes chat requests
- Crawl orchestration → Sitemap discovery: discovers URLs (evidence: `src/services/crawl.service.ts`)
- Crawl orchestration → Safe page fetching: fetches pages (evidence: `src/services/crawl.service.ts`)
- Crawl orchestration → Content extraction: extracts content (evidence: `src/services/crawl.service.ts`)
- Crawl orchestration → Token-aware chunking: chunks content (evidence: `src/services/crawl.service.ts`)
- Crawl orchestration → Embedding generation: generates embeddings (evidence: `src/services/crawl.service.ts`)
- Crawl orchestration → Vector index: stores vectors (evidence: `src/services/crawl.service.ts`)
- Embedding generation → Vector index: provides vectors (evidence: `src/services/qdrant.service.ts`)
- Semantic search → Vector index: queries vectors
- Chat orchestration → Agent runtime: invokes agent (evidence: `src/routes/chatRoutes.ts`)
- Agent runtime → Model selection: selects model (evidence: `src/modules/runtime/agent-runtime.service.ts`)
- Agent runtime → Context window: manages context (evidence: `src/modules/runtime/agent-runtime.service.ts`)
- Agent runtime → Chat persistence: loads conversation (evidence: `src/modules/runtime/agent-runtime.service.ts`)
- Workspace connections → OAuth authorization: starts authorization (evidence: `src/modules/connections/auth.manager.ts`)
- OAuth authorization → Workspace connections: triggers discovery (evidence: `src/modules/connections/auth.manager.ts`)
- Tool execution → MCP client: dispatches MCP tools (evidence: `src/modules/execution/execution-engine.service.ts`)
- Legal source discovery → Legal ingestion: supplies discovered documents (evidence: `src/modules/legal-research/ingestion/legal-one-time-ingestion.service.ts`)
- Legal ingestion → Embedding generation: embeds legal chunks (evidence: `src/modules/legal-research/ingestion/legal-ingestion.service.ts`)
- Legal ingestion → Vector index: indexes legal content (evidence: `src/modules/legal-research/ingestion/legal-ingestion.service.ts`)
- Legal research agent → Legal retrieval: retrieves authorities
