Context Management
Relevant source files
The following files were used as context for generating this wiki page:
- src/prompts/system_prompt.ts
- src/prompts/security_review_prompt.ts
- src/prompts/summarize_chat_system_prompt.ts
- 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
System prompts are the foundational instructions that define the AI's role, behavior, capabilities, and constraints within the Dyad application. They are dynamically constructed based on the current interaction mode (e.g., building code, answering questions, performing a security review) and can be customized on a per-project basis via an AI_RULES.md file. This architecture allows the AI to adapt its personality, tools, and output format to the specific task at hand.
The core logic for assembling these prompts resides in constructSystemPrompt, which selects the appropriate base prompt for the given chatMode and injects project-specific rules. This ensures a consistent yet flexible framework for guiding the AI.
Sources: src/prompts/system_prompt.ts:474-490
Prompt Architecture
The final system prompt sent to the AI is a composite string assembled from several building blocks. The constructSystemPrompt function orchestrates this assembly based on the selected chatMode.
Sources: src/prompts/system_prompt.ts:474-490
Prompt Construction Flow
The following diagram illustrates how a system prompt is constructed, particularly for the "build" mode.
This diagram shows the process of selecting a base prompt, optionally adding other specialized prompts, and injecting project-specific rules to create the final set of instructions for the AI.
Sources: src/prompts/system_prompt.ts:474-506
Key Components
| Component | Description | Source File |
|---|---|---|
BUILD_SYSTEM_PREFIX | Defines the AI's core persona as "Dyad, an AI editor," and outlines guidelines for code generation, using commands (<dyad-command>), and interacting with the user. | src/prompts/system_prompt.ts:70-244 |
BUILD_SYSTEM_POSTFIX | Contains non-negotiable rules, primarily enforcing the exclusive use of <dyad-write> tags for all code output and prohibiting Markdown code blocks. | src/prompts/system_prompt.ts:303-317 |
THINKING_PROMPT | Instructs the AI to use a structured <think> block to plan its approach before responding, ensuring a more organized and thorough problem-solving process. | src/prompts/system_prompt.ts:5-68 |
[[AI_RULES]] | A placeholder within the main prompt templates that is replaced by the content of AI_RULES.md or DEFAULT_AI_RULES. | src/prompts/system_prompt.ts:321-323 |
DEFAULT_AI_RULES | A fallback set of rules defining the default tech stack (React, TypeScript, shadcn/ui) and file structure conventions. | src/prompts/system_prompt.ts:325-345 |
AI Chat Modes
Dyad employs several distinct chat modes, each with a specialized system prompt that tailors the AI's behavior to a specific task. The mode is determined by the user's interaction or intent.
Sources: src/prompts/system_prompt.ts:480-483
Build Mode
This is the default and primary mode for application development. The AI acts as a software engineer, writing and modifying code in response to user requests.
- Role: "Dyad, an AI editor that creates and modifies web applications."
- Primary Directives:
- Generate fully functional, complete code.
- Use
<dyad-write>,<dyad-rename>,<dyad-delete>, and<dyad-add-dependency>tags for all file system operations. - Provide concise, non-technical summaries of changes.
- Follow project-specific
AI_RULES.md. - Never use Markdown for code;
<dyad-write>is mandatory.
Sources: src/prompts/system_prompt.ts:70-317, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---default-build-mode-1.txt
Ask Mode
In this mode, the AI acts as a helpful technical guide, providing explanations and advice without writing any code.
- Role: "A helpful AI assistant that specializes in web development, programming, and technical guidance."
- Primary Directives:
- ABSOLUTE PRIMARY DIRECTIVE: YOU MUST NOT, UNDER ANY CIRCUMSTANCES, WRITE OR GENERATE CODE.
- Explain concepts, patterns, and best practices.
- Do not use
<dyad-*>tags. - Focus on conceptual explanations and avoid implementation details.
Sources: src/prompts/system_prompt.ts:347-418, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
Agent Mode
This mode is for analyzing user requests to determine if external tools or information are needed before development can begin.
- Role: "An AI App Builder Agent."
- Primary Directives:
- Determine necessary tools, APIs, or data for a request.
- ABSOLUTELY NO CODE GENERATION.
- If no tools are needed, respond with: "Ok, looks like I don't need any tools, I can start building."
- Do not use
<dyad-*>tags.
Sources: src/prompts/system_prompt.ts:420-472
Local Agent Mode
This mode enables the AI to interact with the user's local environment through a specific set of tools. The prompt is constructed by constructLocalAgentPrompt and includes rules for tool calling, such as reading files before writing and using search_replace for edits.
Sources: src/prompts/system_prompt.ts:480-482, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
Specialized Prompts
Beyond the primary chat modes, Dyad uses highly specialized prompts for specific, automated tasks like security reviews and chat summarization.
Security Review Prompt
This prompt transforms the AI into a security expert tasked with identifying vulnerabilities in the codebase.
- Role: "Security expert identifying vulnerabilities that could lead to data breaches, leaks, or unauthorized access."
- Focus Areas: Authentication, Injection Attacks, API Security, and Client-Side Secrets.
- Output Format: Must use the
<dyad-security-finding>XML tag with a specific structure.
The structure of the output is strictly defined as follows:
<dyad-security-finding title="Brief title" level="critical|high|medium|low">
**What**: Plain-language explanation
**Risk**: Data exposure impact (e.g., "All customer emails could be stolen")
**Potential Solutions**: Options ranked by how effectively they address the issue
**Relevant Files**: Relevant file paths
</dyad-security-finding>
Sources: src/prompts/security_review_prompt.ts:1-53, e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
Chat Summarization Prompt
This prompt is used to generate a concise summary of a chat session, focusing on technical outcomes.
- Role: "A helpful assistant that summarizes AI coding chat sessions."
- Task: Analyze the conversation and provide a summary, a list of major changes, and a list of relevant files.
- Output Format: A specific Markdown structure ending with a mandatory
<dyad-chat-summary>tag.
Sources: src/prompts/summarize_chat_system_prompt.ts:1-36
AI_RULES.md Integration
The AI_RULES.md file provides a powerful mechanism for customizing the AI's behavior on a per-project basis. The system reads this file and injects its contents into the system prompt.
Loading and Fallback
The readAiRules function attempts to read AI_RULES.md from the project's root directory. If the file cannot be read (e.g., it doesn't exist), the system falls back to using a predefined DEFAULT_AI_RULES string.
This flow is visualized in the sequence diagram below:
Sources: src/prompts/system_prompt.ts:508-517
Default Rules
When AI_RULES.md is not present, the following default rules are applied, establishing a standard development environment.
| Rule | Description |
|---|---|
| Framework | React |
| Language | TypeScript |
| Routing | React Router, with routes in src/App.tsx |
| File Structure | Source code in src/, pages in src/pages/, components in src/components/ |
| UI Library | shadcn/ui is the preferred choice |
| Styling | Tailwind CSS for all styling |
| Icons | lucide-react package |
Sources: src/prompts/system_prompt.ts:325-345