The Copilot Context System enables users to attach relevant information to their chat messages by referencing past conversations, workflows, execution logs, knowledge bases, and other workspace resources. This system allows the AI assistant to have access to specific context when answering questions, making responses more accurate and relevant to the user's current work.
This page covers:
processContextsServer.The system supports multiple distinct context types, each providing different information to the AI assistant. These are defined in the ChatContext type.
| Context Type | Kind | Purpose | Data Source |
|---|---|---|---|
| Past Chat | past_chat | Reference previous conversations | copilotChats table |
| Workflow | workflow | Reference any workflow in workspace | workflow table |
| Current Workflow | current_workflow | Reference the currently open workflow | Active workflow state |
| Blocks | blocks | Reference specific blocks from the registry | blockRegistry |
| Workflow Block | workflow_block | Reference specific block instance in a workflow | workflowBlocks table |
| Logs | logs | Reference execution logs | workflowExecutions table |
| Knowledge | knowledge | Reference knowledge base contents | knowledgeBase table |
| Table | table | Reference workspace tables | table table |
| File | file | Reference uploaded workspace files | workspace_files storage |
| Templates | templates | Reference workflow templates | templates table |
| Docs | docs | Reference platform documentation | Documentation index |
Sources:
The ChatContext object is the primary structure used to track selected context items before they are sent to the server.
Sources:
Users select contexts by typing @ in the chat input, which triggers a MentionMenu. This menu displays available context types and items fetched from the backend. The useChat hook manages the state of these contexts and coordinates their submission to the chat APIs.
Mention Menu Architecture
Sources:
When a message is sent with contexts, the chat API routes (POST /api/copilot/chat or POST /api/mothership/chat) process each context server-side using processContextsServer. This function resolves IDs into actual textual content.
Server-Side Context Processing
Sources:
The get_blocks_metadata server tool is used to fetch detailed schemas for specific block types. This metadata includes input definitions, available tools, and trigger configurations, which are essential for the AI to understand how to manipulate or suggest workflow blocks.
Sources:
The Copilot Context System also utilizes a Virtual File System to provide the LLM with access to files and resources within a workspace. Tools like vfs_read, vfs_list, and vfs_grep allow the assistant to explore workspace context dynamically.
| Tool | Function | Description |
|---|---|---|
vfs_read | executeVfsRead | Reads content of a file (workspace or chat upload). |
vfs_list | executeVfsList | Lists directory entries in the VFS. |
vfs_grep | executeVfsGrep | Searches for patterns within VFS files. |
vfs_glob | executeVfsGlob | Finds files matching a pattern. |
Sources:
When the Copilot executes tools (e.g., run_workflow or manage_custom_tool), it operates within an ExecutionContext. This context carries the necessary environment variables and permissions to ensure the tool runs with the correct user scope.
Sources:
| Layer | Symbol | File | Purpose |
|---|---|---|---|
| API | POST /api/copilot/chat | apps/sim/app/api/copilot/chat/route.ts | Main endpoint handling chat requests and context normalization. |
| API | POST /api/mothership/chat | apps/sim/app/api/mothership/chat/route.ts | Workspace-scoped chat endpoint. |
| Hook | useChat | apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts | Client-side manager for chat state and streaming events. |
| Logic | processContextsServer | apps/sim/lib/copilot/process-contents.ts | Resolves abstract context IDs into textual content for the LLM. |
| Logic | getBlocksMetadataServerTool | apps/sim/lib/copilot/tools/server/blocks/get-blocks-metadata-tool.ts | Fetches block definitions and schemas for the AI. |
| Storage | copilotChats | packages/db/src/schema.ts | Database schema for persisting chats and their associated contexts. |
| Tooling | executeVfsRead | apps/sim/lib/copilot/orchestrator/tool-executor/vfs-tools.ts | Tool for the AI to read workspace file context. |
Sources:
Refresh this wiki