This document describes the architectural design of the Copilot AI assistant system, including its backend orchestration, message persistence, agent-to-agent coordination, and streaming infrastructure. Copilot is an intelligent assistant that helps users build, test, deploy, and debug workflows through natural language interaction. It operates in two primary modes: Workflow Copilot (scoped to a specific workflow) and Mothership Chat (workspace-wide assistant).
For information about Copilot's context system and @-mentions, see 6.2 For details on Copilot's server-side tools, see 6.3 For information about Copilot modes and capabilities, see 6.4
Copilot is a full-stack AI assistant built on a client-server architecture with three primary layers:
MothershipChat, Panel) and hooks (useChat) that manage local message state, resource registries, and SSE stream consumption apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:61-85.orchestrateCopilotStream) that proxies requests to the Sim Agent API (Go backend), manages long-running tool executions, and handles async continuations apps/sim/lib/copilot/orchestrator/index.ts82-143The following diagram bridges the "Natural Language Space" (User Input) to the "Code Entity Space" (API Routes and Orchestrators).
Copilot Component Interaction
Sources: apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:12-36, apps/sim/app/api/copilot/chat/route.ts111-126 apps/sim/lib/copilot/orchestrator/index.ts82-143 apps/sim/app/workspace/[workspaceId]/home/home.tsx:156-160
The system exposes two main endpoints for interaction:
/api/copilot/chat): Requires a workflowId. It resolves the workflow scope using resolveWorkflowIdForUser and processes contexts like block outputs or logs specific to that DAG apps/sim/app/api/copilot/chat/route.ts165-176/api/mothership/chat): Workspace-scoped. It uses generateWorkspaceContext to provide a broader view of the environment, including information about all workflows, tables, and files apps/sim/app/api/mothership/chat/route.ts19-23getSession() to ensure the user is authenticated apps/sim/app/api/copilot/chat/route.ts120-126processContextsServer and resolveActiveResourceContext apps/sim/app/api/mothership/chat/route.ts16 apps/sim/app/api/mothership/chat/route.ts178-184acquirePendingChatStream to ensure only one active stream exists per chatId, preventing race conditions apps/sim/lib/copilot/chat-streaming.ts115-119orchestrateCopilotStream to manage the interaction with the LLM and tool execution loop apps/sim/lib/copilot/orchestrator/index.ts82-85Copilot uses Server-Sent Events (SSE) for real-time response delivery. To handle network instability or page refreshes, it implements a persistence-based recovery system.
To prevent multiple orchestrators from writing to the same chat simultaneously, the system uses a Redis-backed lock via getChatStreamLockKey(chatId) apps/sim/lib/copilot/chat-streaming.ts59-61 Locks have a TTL of 2 hours apps/sim/lib/copilot/chat-streaming.ts21
useChat hook maintains a list of messages and a messageQueue for optimistic updates apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:61-81.buildReplayStream to recreate the SSE stream from stored events apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:170-186.eventId of the last received message to resume from the exact point of failure apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:103-107.The orchestrateCopilotStream function manages a complex loop where the LLM can call tools, wait for results, and then continue generating text.
executeToolServerSide apps/sim/lib/copilot/orchestrator/tool-executor/index.ts8-12 This includes workflow-tools (e.g., executeRunWorkflow), vfs-tools (filesystem operations), and deployment-tools apps/sim/lib/copilot/orchestrator/tool-executor/index.ts46-112open_resource) are flagged as clientExecutable. The orchestrator pauses and waits for a tool_result event from the client apps/sim/lib/copilot/orchestrator/sse/handlers/handlers.ts99-112async_pause. The state is saved as a checkpoint, and execution resumes when the tool completes via a background worker apps/sim/lib/copilot/orchestrator/index.ts165-178Copilot supports nested agents (sub-agents). The orchestrator tracks this using subagent identifiers in the SSE stream apps/sim/lib/copilot/orchestrator/sse/handlers/handlers.ts22 The UI handles these transitions by grouping blocks under specific agent labels apps/sim/app/workspace/[workspaceId]/home/components/message-content/message-content.tsx:138-169.
Tool Execution Sequence
Sources: apps/sim/lib/copilot/orchestrator/index.ts183-197 apps/sim/lib/copilot/orchestrator/sse/handlers/tool-execution.ts150-158 apps/sim/lib/copilot/orchestrator/tool-executor/index.ts108-112
Messages are stored in a structured format in the database (TaskStoredMessage) and mapped to a UI-friendly format (ChatMessage) by the useChat hook apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:27-36.
<thinking> tags are extracted and rendered as reasoning phases apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:189-194.The MessageContent component parses raw content blocks into segments:
options tag apps/sim/app/workspace/[workspaceId]/home/components/message-content/message-content.tsx:35-38.Sources: apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts:188-216, apps/sim/app/workspace/[workspaceId]/home/components/message-content/message-content.tsx:89-109, apps/sim/app/workspace/[workspaceId]/home/types.ts:1-20
Refresh this wiki