Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:
- src/prompts/system_prompt.ts
- e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
- e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
- e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
- e2e-tests/snapshots/mention_files.spec.ts_mention-file-1.txt
- e2e-tests/snapshots/dump_messages.spec.ts_dump-messages-1.txt
The Dyad application is an AI-powered code editor designed to assist users in creating and modifying web applications through a chat interface. Its architecture is centered around a sophisticated system of dynamic AI prompts that vary based on the user's selected "chat mode." This allows the AI to adopt different roles, such as a code builder, a technical advisor, or a security expert.
The core of the system is a prompt-construction mechanism that assembles a detailed system prompt for the AI. This prompt dictates the AI's role, capabilities, rules, and output format. The AI interacts with the user's codebase by generating specific XML-like commands, such as <dyad-write> or <dyad-add-dependency>, which are then interpreted by the application to perform file system operations. This command-based interaction model decouples the AI's reasoning from the direct execution of code changes, providing a structured and controlled workflow.
AI System Prompts
The behavior of the Dyad AI is fundamentally controlled by a system prompt dynamically generated based on the current chatMode. The constructSystemPrompt function is the main entry point for this process.
Sources: src/prompts/system_prompt.ts:503-518
Prompt Construction Flow
The system constructs the final prompt by selecting a base template corresponding to the active chat mode and injecting project-specific rules.
This diagram shows the logic for building the final system prompt based on the selected chat mode and the presence of a custom AI_RULES.md file.
Sources: src/prompts/system_prompt.ts:503-537
Chat Modes
The application supports several distinct chat modes, each with a specialized system prompt that defines the AI's persona and capabilities.
| Mode | Description | System Prompt Constant |
|---|---|---|
build | The default mode. The AI acts as a developer, writing and modifying code using <dyad-*> commands. It is instructed to be simple, elegant, and follow best practices. | BUILD_SYSTEM_PROMPT |
ask | The AI acts as a helpful assistant for technical guidance. It is strictly forbidden from writing any code or using <dyad-*> tags. Its sole purpose is to explain concepts. | ASK_MODE_SYSTEM_PROMPT |
agent | The AI acts as an "App Builder Agent" to analyze requests and determine necessary tools or APIs. It does not write code but gathers information for the development phase. | AGENT_MODE_SYSTEM_PROMPT |
local-agent | A mode that uses a different prompt construction function (constructLocalAgentPrompt) for tasks that may involve local tool usage. | constructLocalAgentPrompt |
security-review | A specialized mode where the AI acts as a security expert to identify vulnerabilities. It uses a unique output format (<dyad-security-finding>). | (Defined in snapshot tests) |
Sources: src/prompts/system_prompt.ts:503-537, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt, e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
AI Rules (AI_RULES.md)
The prompting system is designed to be customizable on a per-project basis through an AI_RULES.md file.
- The
readAiRulesfunction attempts to readAI_RULES.mdfrom the user's project path. - If the file is not found, it falls back to a
DEFAULT_AI_RULESconstant. - This content is then injected into the main system prompt, replacing the
[[AI_RULES]]placeholder.
Sources: src/prompts/system_prompt.ts:516-518, src/prompts/system_prompt.ts:539-550
The default rules specify the tech stack and available libraries:
const DEFAULT_AI_RULES = `# Tech Stack
- You are building a React application.
- Use TypeScript.
- Use React Router. KEEP the routes in src/App.tsx
- Always put source code in the src folder.
- Put pages into src/pages/
- Put components into src/components/
- The main page (default page) is src/pages/Index.tsx
- UPDATE the main page to include the new components. OTHERWISE, the user can NOT see any components!
- ALWAYS try to use the shadcn/ui library.
- Tailwind CSS: always use Tailwind CSS for styling components. Utilize Tailwind classes extensively for layout, spacing, colors, and other design aspects.
Available packages and libraries:
- The lucide-react package is installed for icons.
- You ALREADY have ALL the shadcn/ui components and their dependencies installed. So you don't need to install them again.
- You have ALL the necessary Radix UI components installed.
- Use prebuilt components from the shadcn/ui library after importing them. Note that these files shouldn't be edited, so make new components if you need to change them.
`;
Sources: src/prompts/system_prompt.ts:391-417
AI Command-Based Interaction
In build mode, the AI does not have direct access to the file system. Instead, it issues commands using custom XML-like tags. The application is responsible for parsing these tags and executing the corresponding actions.
This diagram illustrates the sequence of a user making a request and the AI responding with commands.
Sources: src/prompts/system_prompt.ts:70-296
Core Commands
The following commands are the primary mechanism for the AI to interact with the project.
| Command | Attributes | Description |
|---|---|---|
<dyad-write> | path, description | Creates a new file or completely overwrites an existing one. The AI is instructed to always write the entire file. |
<dyad-rename> | from, to | Renames a file or directory. |
<dyad-delete> | path | Deletes a file. |
<dyad-add-dependency> | packages | Adds one or more npm packages to the project. Package names are space-separated. |
<dyad-command> | type | Suggests that the user run a UI command (rebuild, restart, refresh). |
<dyad-chat-summary> | Sets a concise title for the current chat session. | |
<think> | A block for the AI to outline its plan before generating the final response. This is not shown to the user. |
Sources: src/prompts/system_prompt.ts:8-296
Specialized Commands
Certain modes introduce their own commands or output formats.
- Security Review Mode: Uses the
<dyad-security-finding>tag to report vulnerabilities in a structured format.
Sources: e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt - Ask Mode: Can use
<dyad-add-integration provider="supabase">to suggest adding integrations to the user.
Sources: e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt