How ssc-dsai/canchat-agent works
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: agentRuntime.ts Agent runtime · Sidebar.tsx Sidebar chat UI · automation.ts Browser automation · serviceWorker.ts Service worker
Components 20
Extension UI
- Extension manifest
public/manifest.json - Sidebar chat UI · Conversation, settings, plans, tools, and approval cards.
src/sidebar/Sidebar.tsx - Chat panel
src/sidebar/ChatPanel.tsx - Plan panel
src/sidebar/PlanPanel.tsx - Workspace UI · Manages persisted data and automations.
src/workspace/Workspace.tsx
Background Agent
- Service worker · Routes UI requests, maintains ports, and seeds extension state.
src/background/serviceWorker.ts - Agent runtime · Runs think-act-observe with plans, budgets, context compaction, and pauses.
src/background/agentRuntime.ts - Model provider · Calls configured chat, embedding, and transcription endpoints.
src/background/llmProvider.ts - Browser tool adapter
src/background/browserToolAdapter.ts - Tool contracts
src/shared/unifiedTools.ts
Browser Interaction
- Browser automation · Injects and messages page-local browser tools.
src/background/automation.ts - Content script · Extracts page content, element maps, and performs synthetic interaction.
src/content/contentScript.ts - Auth and permissions · Converts login and site-access failures into resumable pauses.
src/background/authDetector.ts
Local Knowledge
- Extension storage · Stores configuration, conversations, skills, sites, memory, and schedules.
src/background/storage.ts - Repository ingestion · Chunks web, PDF, and Office content and requests embeddings.
src/background/repoIngest.ts - Memory index · Stores and searches on-device vectors.
src/background/memoryIndex.ts - Offscreen processing · Hosts DuckDB and heavy document extraction.
src/offscreen/offscreen.ts
Integrations
- External integrations · SharePoint, MCP HTTP, and page-provided WebMCP access.
src/background/mcpClient.ts - Configured model endpoint · User-configured OpenAI-compatible endpoint.
- Word add-in · Imports backup-compatible knowledge and settings.
word-addin/src/main.tsx
Connections 22
- 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