How perfectthymetech/m365-custom-agent works
1. Repository shape
- This is a three-part repo: code/copilot contains the runtime service, code/infra contains Azure Terraform, and docs/ contains deployment and setup guidance. code/teams/manifest.json is the Teams app packaging boundary.
2. Primary runtime
- The application is a Python FastAPI backend (code/copilot/app/main.py) packaged with uv/Poetry-style dependency management and run in a container (Dockerfile). The README positions it as the reference implementation for the Microsoft 365 Agents SDK.
3. HTTP API boundary
- The explicit API surface is versioned under app/api/v1/, with endpoints for:
- heartbeat for service health/readiness
- messages for conversational interaction
- This suggests the service exposes a lightweight REST layer in front of the agent/coprocessing logic.
4. Agent orchestration layer
- app/agents/ contains reusable agent behaviors: root, document, summarizer, and actions.
- These are the main business-logic units that likely decide how a request is interpreted, whether a document-oriented workflow is needed, and how results are summarized or actioned.
5. Microsoft 365 Agents SDK / channel integration
- The app/copilot/ package is the channel and SDK integration layer.
- It includes:
- channel-specific handlers for Microsoft Teams and web chat
- activity adapters for those channels
- shared copilot configuration, scenario routing, and common abstractions
- handler_abstract.py and the concrete channel handlers indicate a polymorphic boundary between inbound activities and agent execution.
6. Scenario-driven behavior
- The presence of app/copilot/scenarios.py, code/schemas/scenarios.schema.json, and docs/SCENARIO_DEFINITIONS.yaml shows the app is not a single-purpose bot; it is driven by configurable scenarios.
- That configuration boundary likely maps user messages to predefined workflows, prompts, or tool-use patterns.
7. Document and attachment pipeline
- app/files/compression.py and app/files/extraction.py, plus app/models/attachments.py, indicate the service can accept and process files.
- This is an important subsystem boundary: inbound messages may carry attachments, which are then compressed/extracted before being handed to document-centric agents.
8. Domain models
- app/models/ defines contracts for the service’s core entities: agents, copilot state, heartbeat, scenarios, and attachments.
- These models likely sit between the API layer, the agent layer, and the channel adapters, providing typed data exchange within the app.
9. Configuration, logging, and runtime settings
- app/core/settings.py, app/copilot/configuration.py, and app/logs.py centralize environment-driven settings and observability setup.
- The README calls out logging/monitoring via OpenTelemetry, so telemetry is a first-class cross-cutting concern rather than an afterthought.
10. Azure infrastructure footprint
- code/infra/ defines the deployment target and dependencies:
- Azure Web App for Linux for hosting the backend
- App Service Plan
- Bot Service for channel connectivity
- Cosmos DB for persistent state/data
- Storage Account for files or artifacts
- Key Vault for secrets
- Azure OpenAI and Document Intelligence as external AI/document services
- Application Insights for telemetry
- Managed Identity and role assignments for secure resource access
- The network and DNS Terraform files show the stack is designed for controlled Azure networking, not a simple public-only deployment.
11. Deployment and environment separation
- There are two Terraform layers:
- docs/prereqs/ for shared prerequisite infrastructure
- code/infra/ for the application environment itself
- config/PerfectThymeTech/*.tfvars suggests named environment configuration, and the GitHub Actions workflows indicate automated linting, Terraform, and webapp deployment pipelines.
12. Teams packaging
- code/teams/manifest.json is the distribution boundary for Microsoft Teams installation.
- Combined with the bot service and Teams handler code, this repo supports a Teams-connected agent rather than just a standalone API.
13. Test and quality boundary
- code/copilot/tests/ and the top-level CI files indicate the service has automated validation around the app layer, while pre-commit and lint workflows enforce code quality before deployment.
14. How data moves through the system
- A user message arrives from Teams or web chat, is normalized by the channel handler/activity layer, routed through scenario/agent logic, may invoke file extraction or document workflows, and then returns a response through the same channel boundary.
- Separately, direct API calls can hit the FastAPI endpoints for health checks or message handling.
15. What to draw in an architecture diagram
- The key boxes are: client channels (Teams/web chat), bot/channel adapter layer, FastAPI service, agent/scenario orchestration, file-processing subsystem, and Azure dependencies (OpenAI, Document Intelligence, Cosmos DB, Storage, Key Vault, App Insights).
- The main trust boundaries are external channels, the API service, and the Azure resource boundary enforced by managed identity and secrets in Key Vault.
Main entry points: copilot.py Copilot core · webapplinux.tf Web App · root.py Root agent · main.py Main
Components 26
App runtime
Python FastAPI service and copilot logic
- Main · Application bootstrap and server startup
code/copilot/app/main.py - API v1 · Versioned HTTP surface for service operations
code/copilot/app/api/v1/router.py - Heartbeat · Readiness and service health checks
code/copilot/app/api/v1/endpoints/heartbeat.py - Messages · Conversation entrypoint for user requests
code/copilot/app/api/v1/endpoints/messages.py - Copilot core · Shared activity handling and scenario routing
code/copilot/app/copilot/copilot.py - Handler base · Abstract boundary for channel-specific handlers
code/copilot/app/copilot/handler_abstract.py - Teams handler · Microsoft Teams activity processing
code/copilot/app/copilot/handler_msteams.py - Webchat handler · Web chat activity processing
code/copilot/app/copilot/handler_webchat.py - Scenarios · Scenario-driven workflow selection and configuration
code/copilot/app/copilot/scenarios.py - Root agent · Top-level request interpretation and dispatch
code/copilot/app/agents/root.py - Document agent · Document-oriented workflow handling
code/copilot/app/agents/document.py - Summarizer · Response condensation and synthesis
code/copilot/app/agents/summarizer.py - Actions · Action execution and tool-oriented behavior
code/copilot/app/agents/actions.py - File pipeline · Attachment compression and extraction flow
code/copilot/app/files/extraction.py - Domain models · Typed service entities for copilot, scenarios, attachments, and heartbeat
code/copilot/app/models/copilot.py - Settings · Environment-driven runtime configuration
code/copilot/app/core/settings.py - Telemetry · Logging and OpenTelemetry setup
code/copilot/app/logs.py
Azure infra
Terraform-managed deployment and platform dependencies
- Web App · Container hosting for the FastAPI service
code/infra/webapplinux.tf - Bot Service · Channel connectivity for Teams and bot traffic
code/infra/botservice.tf - Data stores · Persistent state, artifacts, and file storage
code/infra/cosmosdb.tf - AI services · Model inference and document intelligence dependencies
code/infra/aoai.tf - Secrets and identity · Managed identity, secret access, and role assignments
code/infra/keyvault.tf - App Insights · Application monitoring and traces
code/infra/applicationinsights.tf
Docs and packaging
Repository docs, Teams manifest, and scenario definitions
- Teams manifest · Microsoft Teams app packaging and distribution
code/teams/manifest.json - Scenario defs · Repository-level scenario configuration contract
code/schemas/scenarios.schema.json - Deploy docs · Deployment, setup, and scenario guidance
docs/README.md
Connections 27
- Main → API v1: mounts · Bootstraps the versioned HTTP API
- Main → Copilot core: starts · Initializes the copilot runtime
- API v1 → Heartbeat: exposes · Health and readiness endpoint
- API v1 → Messages: exposes · Conversation endpoint
- Messages → Copilot core: dispatches · Hands message requests to orchestration
- Copilot core → Handler base: uses · Relies on abstract channel handling
- Handler base → Teams handler: implements · Teams-specific adapter
- Handler base → Webchat handler: implements · Web chat adapter
- Copilot core → Scenarios: routes by · Scenario-driven workflow selection
- Scenarios → Root agent: selects · Primary request entry agent
- Root agent → Document agent: delegates · Document-centric workflows
- Root agent → Summarizer: delegates · Response summarization
- Root agent → Actions: delegates · Action-oriented execution
- Messages → File pipeline: attaches · Attachment preprocessing for inbound messages
- File pipeline → Document agent: feeds · Processed file content enters document flow
- Copilot core → Domain models: shares · Uses typed internal contracts
- Copilot core → Settings: reads · Loads runtime configuration
- Copilot core → Telemetry: emits · Sends telemetry and logs
- Main → Web App: deploys to · Runs as a Linux container app
- Web App → Bot Service: connects · Receives channel traffic through bot service
- Web App → Data stores: persists to · Uses Cosmos DB and storage
- Web App → AI services: calls · Invokes OpenAI and document intelligence
- Web App → Secrets and identity: auth via · Uses managed identity and secrets
- Web App → App Insights: reports · Application telemetry sink
- Teams manifest → Bot Service: packages for · Teams app boundary for bot connectivity
- Scenario defs → Scenarios: defines · Scenario configuration contract
- Deploy docs → Web App: describes · Deployment guidance for the runtime