# ssc-dsai/canchat-agent architecture

> Architecture diagram of the GitHub repository ssc-dsai/canchat-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ssc-dsai/canchat-agent
- Repository: https://github.com/ssc-dsai/canchat-agent

## Overview

#### Purpose and Entry Points
CANChat Agent is a Chromium Manifest V3 browser agent. Its primary UI is a side-panel chat that uses a user-configured OpenAI-compatible model to reason over and operate the user’s browser session, with approval gates for state-changing actions. The extension manifest and page entry points are `public/manifest.json`, `sidebar.html`, and `src/sidebar/main.tsx`. Supporting standalone extension surfaces include `offscreen.html`, `microphone.html`, `workspace.html`, and `map.html`.

#### UI and User-Control Boundary
The Preact sidebar owns conversation presentation, settings, plan visibility, tool activity, and approval/auth/permission cards. It communicates with the service worker rather than calling browser APIs directly. Key surfaces are `src/sidebar/Sidebar.tsx`, `src/sidebar/ChatPanel.tsx`, and `src/sidebar/PlanPanel.tsx`. A separate workspace UI manages persisted data and automations at `src/workspace/Workspace.tsx`.

#### Background Agent Runtime
The service worker is the central control plane: it receives sidebar events, maintains ports, seeds state, and routes requests. The agent runtime executes a model-driven think→act→observe loop, maintains plans/findings/step budgets, compacts context, and suspends for approvals or login. Core paths: `src/background/serviceWorker.ts`, `src/background/agentRuntime.ts`, `src/background/loopHelpers.ts`.

#### Model and Tool Execution
`src/background/llmProvider.ts` calls the user’s OpenAI-compatible chat, embedding, and transcription endpoints with timeout/abort behavior. The runtime exposes shared tool schemas and dispatches browser-oriented tools through `src/background/browserToolAdapter.ts`; common contracts and tool definitions are in `src/shared/schemas.ts` and `src/shared/unifiedTools.ts`. The model endpoint is the primary external service; no application backend is implied.

#### Browser Interaction Pipeline
Browser reads and actions cross a privilege boundary: the background layer injects or messages content scripts, while page-local code extracts DOM/readability content, builds accessibility-aware element maps, and performs synthetic interactions. Relevant paths are `src/content/contentScript.ts`, `src/content/domExtractor.ts`, and `src/background/automation.ts`. Authentication and site-access failures are detected centrally and returned to the sidebar as resumable pauses through `src/background/authDetector.ts` and `src/background/permissions.ts`.

#### Local Knowledge, Storage, and Retrieval
User configuration, conversations, skills, known sites, memory, and scheduling state are persisted through `src/background/storage.ts`. Local repositories ingest web/PDF/Office content, chunk it, request embeddings from the configured endpoint, and store/search vectors on-device; ingestion and indexing live in `src/background/repoIngest.ts` and `src/background/memoryIndex.ts`. Shared retrieval logic includes `src/shared/hybridSearch.ts` and `src/shared/vectorSearch.ts`. The offscreen document hosts heavier local processing, including DuckDB and document extraction: `src/offscreen/offscreen.ts`, `src/offscreen/duckDb.ts`, `src/offscreen/docGen.ts`.

#### Integrations and Secondary Clients
The extension can use existing browser-authenticated sessions for SharePoint/Microsoft Graph-style access, MCP HTTP tools, and page-provided WebMCP tools. Integration clients are `src/background/sharepointIngest.ts`, `src/background/mcpClient.ts`, and `src/content/webmcpBridge.ts`. A companion Microsoft Word task-pane add-in imports backup-compatible knowledge/settings and has its own entry point at `word-addin/src/main.tsx`, with document integration in `word-addin/src/wordDoc.ts`.

Main entry points: [`src/background/agentRuntime.ts`](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/agentRuntime.ts) (Agent runtime), [`src/sidebar/Sidebar.tsx`](https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/Sidebar.tsx) (Sidebar chat UI), [`src/background/automation.ts`](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/automation.ts) (Browser automation), [`src/background/serviceWorker.ts`](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/serviceWorker.ts) (Service worker)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Extension UI"]
  node_manifest["Extension manifest<br/>MV3 entry<br/>[manifest.json]"]
  node_sidebar["Sidebar chat UI<br/>Preact UI<br/>[Sidebar.tsx]"]
  node_chat_panel["Chat panel<br/>UI surface<br/>[ChatPanel.tsx]"]
  node_plan_panel["Plan panel<br/>UI surface<br/>[PlanPanel.tsx]"]
  node_workspace["Workspace UI<br/>Preact UI<br/>[Workspace.tsx]"]
end

subgraph group_agent["Background Agent"]
  node_service_worker{{"Service worker<br/>control plane<br/>[serviceWorker.ts]"}}
  node_agent_runtime["Agent runtime<br/>agent loop<br/>[agentRuntime.ts]"]
  node_llm_provider["Model provider<br/>OpenAI-compatible client<br/>[llmProvider.ts]"]
  node_tool_adapter["Browser tool adapter<br/>tool dispatcher"]
  node_tool_contracts["Tool contracts<br/>shared schemas<br/>[unifiedTools.ts]"]
end

subgraph group_browser["Browser Interaction"]
  node_automation["Browser automation<br/>privileged executor<br/>[automation.ts]"]
  node_content_script["Content script<br/>page runtime<br/>[contentScript.ts]"]
  node_auth_permissions["Auth and permissions<br/>pause detector<br/>[authDetector.ts]"]
end

subgraph group_knowledge["Local Knowledge"]
  node_storage[("Extension storage<br/>local persistence<br/>[storage.ts]")]
  node_repository_ingest["Repository ingestion<br/>ingestion pipeline<br/>[repoIngest.ts]"]
  node_memory_index[("Memory index<br/>local vector index<br/>[memoryIndex.ts]")]
  node_offscreen["Offscreen processing<br/>document worker<br/>[offscreen.ts]"]
end

subgraph group_integrations["Integrations"]
  node_external_integrations["External integrations<br/>integration clients<br/>[mcpClient.ts]"]
  node_model_endpoint(("Configured model endpoint<br/>external AI service"))
  node_word_addin["Word add-in<br/>secondary client<br/>[main.tsx]"]
end

node_manifest -->|"loads"| node_sidebar
node_sidebar -->|"renders"| node_chat_panel
node_sidebar -->|"renders"| node_plan_panel
node_sidebar -->|"port events"| node_service_worker
node_workspace -->|"manages data"| node_service_worker
node_service_worker -->|"routes requests"| node_agent_runtime
node_agent_runtime -->|"reasoning requests"| node_llm_provider
node_llm_provider -->|"chat, embeddings, transcription"| node_model_endpoint
node_agent_runtime -->|"executes tools"| node_tool_adapter
node_tool_adapter -->|"uses schemas"| node_tool_contracts
node_tool_adapter -->|"browser tools"| node_automation
node_automation -->|"injects or messages"| node_content_script
node_content_script -->|"DOM observations"| node_automation
node_automation -->|"access failures"| node_auth_permissions
node_auth_permissions -->|"resumable pause"| node_service_worker
node_agent_runtime -->|"persists state"| node_storage
node_repository_ingest -->|"embedding requests"| node_llm_provider
node_repository_ingest -->|"indexed chunks"| node_memory_index
node_memory_index -->|"retrieval context"| node_agent_runtime
node_repository_ingest -->|"document processing"| node_offscreen
node_agent_runtime -->|"integration tools"| node_external_integrations
node_word_addin -->|"imports knowledge and settings"| node_storage

click node_manifest "https://github.com/ssc-dsai/canchat-agent/blob/main/public/manifest.json"
click node_sidebar "https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/Sidebar.tsx"
click node_chat_panel "https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/ChatPanel.tsx"
click node_plan_panel "https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/PlanPanel.tsx"
click node_workspace "https://github.com/ssc-dsai/canchat-agent/blob/main/src/workspace/Workspace.tsx"
click node_service_worker "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/serviceWorker.ts"
click node_agent_runtime "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/agentRuntime.ts"
click node_llm_provider "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/llmProvider.ts"
click node_tool_adapter "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/browserToolAdapter.ts"
click node_tool_contracts "https://github.com/ssc-dsai/canchat-agent/blob/main/src/shared/unifiedTools.ts"
click node_automation "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/automation.ts"
click node_content_script "https://github.com/ssc-dsai/canchat-agent/blob/main/src/content/contentScript.ts"
click node_auth_permissions "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/authDetector.ts"
click node_storage "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/storage.ts"
click node_repository_ingest "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/repoIngest.ts"
click node_memory_index "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/memoryIndex.ts"
click node_offscreen "https://github.com/ssc-dsai/canchat-agent/blob/main/src/offscreen/offscreen.ts"
click node_external_integrations "https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/mcpClient.ts"
click node_word_addin "https://github.com/ssc-dsai/canchat-agent/blob/main/word-addin/src/main.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_manifest,node_sidebar,node_chat_panel,node_plan_panel,node_workspace toneBlue
class node_service_worker,node_agent_runtime,node_llm_provider,node_tool_adapter,node_tool_contracts toneAmber
class node_automation,node_content_script,node_auth_permissions toneMint
class node_storage,node_repository_ingest,node_memory_index,node_offscreen toneRose
class node_external_integrations,node_model_endpoint,node_word_addin toneIndigo
```

## Components

### Extension UI

- [Extension manifest](https://github.com/ssc-dsai/canchat-agent/blob/main/public/manifest.json): MV3 entry · `public/manifest.json`
- [Sidebar chat UI](https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/Sidebar.tsx): Preact UI · Conversation, settings, plans, tools, and approval cards. · `src/sidebar/Sidebar.tsx`
- [Chat panel](https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/ChatPanel.tsx): UI surface · `src/sidebar/ChatPanel.tsx`
- [Plan panel](https://github.com/ssc-dsai/canchat-agent/blob/main/src/sidebar/PlanPanel.tsx): UI surface · `src/sidebar/PlanPanel.tsx`
- [Workspace UI](https://github.com/ssc-dsai/canchat-agent/blob/main/src/workspace/Workspace.tsx): Preact UI · Manages persisted data and automations. · `src/workspace/Workspace.tsx`

### Background Agent

- [Service worker](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/serviceWorker.ts): control plane · Routes UI requests, maintains ports, and seeds extension state. · `src/background/serviceWorker.ts`
- [Agent runtime](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/agentRuntime.ts): agent loop · Runs think-act-observe with plans, budgets, context compaction, and pauses. · `src/background/agentRuntime.ts`
- [Model provider](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/llmProvider.ts): OpenAI-compatible client · Calls configured chat, embedding, and transcription endpoints. · `src/background/llmProvider.ts`
- [Browser tool adapter](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/browserToolAdapter.ts): tool dispatcher · `src/background/browserToolAdapter.ts`
- [Tool contracts](https://github.com/ssc-dsai/canchat-agent/blob/main/src/shared/unifiedTools.ts): shared schemas · `src/shared/unifiedTools.ts`

### Browser Interaction

- [Browser automation](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/automation.ts): privileged executor · Injects and messages page-local browser tools. · `src/background/automation.ts`
- [Content script](https://github.com/ssc-dsai/canchat-agent/blob/main/src/content/contentScript.ts): page runtime · Extracts page content, element maps, and performs synthetic interaction. · `src/content/contentScript.ts`
- [Auth and permissions](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/authDetector.ts): pause detector · Converts login and site-access failures into resumable pauses. · `src/background/authDetector.ts`

### Local Knowledge

- [Extension storage](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/storage.ts): local persistence · Stores configuration, conversations, skills, sites, memory, and schedules. · `src/background/storage.ts`
- [Repository ingestion](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/repoIngest.ts): ingestion pipeline · Chunks web, PDF, and Office content and requests embeddings. · `src/background/repoIngest.ts`
- [Memory index](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/memoryIndex.ts): local vector index · Stores and searches on-device vectors. · `src/background/memoryIndex.ts`
- [Offscreen processing](https://github.com/ssc-dsai/canchat-agent/blob/main/src/offscreen/offscreen.ts): document worker · Hosts DuckDB and heavy document extraction. · `src/offscreen/offscreen.ts`

### Integrations

- [External integrations](https://github.com/ssc-dsai/canchat-agent/blob/main/src/background/mcpClient.ts): integration clients · SharePoint, MCP HTTP, and page-provided WebMCP access. · `src/background/mcpClient.ts`
- **Configured model endpoint**: external AI service · User-configured OpenAI-compatible endpoint.
- [Word add-in](https://github.com/ssc-dsai/canchat-agent/blob/main/word-addin/src/main.tsx): secondary client · Imports backup-compatible knowledge and settings. · `word-addin/src/main.tsx`

## Connections

- Extension manifest → Sidebar chat UI: loads
- Sidebar chat UI → Chat panel: renders
- Sidebar chat UI → Plan panel: renders
- Sidebar chat UI → Service worker: port events
- Workspace UI → Service worker: manages data
- Service worker → Agent runtime: routes requests
- Agent runtime → Model provider: reasoning requests
- Model provider → Configured model endpoint: chat, embeddings, transcription
- Agent runtime → Browser tool adapter: executes tools
- Browser tool adapter → Tool contracts: uses schemas
- Browser tool adapter → Browser automation: browser tools
- Browser automation → Content script: injects or messages
- Content script → Browser automation: DOM observations
- Browser automation → Auth and permissions: access failures
- Auth and permissions → Service worker: resumable pause
- Agent runtime → Extension storage: persists state
- Repository ingestion → Model provider: embedding requests
- Repository ingestion → Memory index: indexed chunks
- Memory index → Agent runtime: retrieval context
- Repository ingestion → Offscreen processing: document processing
- Agent runtime → External integrations: integration tools
- Word add-in → Extension storage: imports knowledge and settings
