State Management
Relevant source files
The following files were used as context for generating this wiki page:
- src/prompts/system_prompt.ts
- 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/security_review.spec.ts_security-review-1.txt
- e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---default-build-mode-1.txt
- e2e-tests/snapshots/problems.spec.ts_problems-auto-fix---gives-up-after-2-attempts-1.txt
System prompts are the foundational instructions that define the AI's role, capabilities, constraints, and output format. They are crucial for guiding the AI's behavior and ensuring it performs its tasks correctly and consistently across different operational contexts. The system dynamically constructs these prompts based on the selected chatMode and project-specific rules.
This document outlines the architecture of the system prompt generation, the different prompt modes, and the key instructions that govern the AI's responses.
Prompt Construction Flow
The primary function for assembling the final system prompt is constructSystemPrompt. This function takes the chatMode and other parameters to select and combine various prompt components, including project-specific AI_RULES.
The following diagram illustrates the logic for constructing a system prompt.
This diagram shows how constructSystemPrompt selects a base prompt based on the chatMode, reads the AI_RULES.md file (or uses a default), and injects these rules into the base prompt to create the final set of instructions for the AI.
Sources: src/prompts/system_prompt.ts:488-506, src/prompts/system_prompt.ts:522-533
AI Rules (AI_RULES.md)
The system allows for project-specific AI rules defined in an AI_RULES.md file at the root of the user's project. The readAiRules function attempts to read this file. If it doesn't exist, it falls back to a DEFAULT_AI_RULES constant. These rules typically define the tech stack, preferred libraries, and coding conventions.
Sources: src/prompts/system_prompt.ts:522-533
Default AI Rules
When AI_RULES.md is not present, the following default rules are applied:
| Rule Category | Guideline |
|---|---|
| Framework | Build a React application using TypeScript. |
| Routing | Use React Router, keeping routes in src/App.tsx. |
| File Structure | Source code in src/, pages in src/pages/, components in src/components/. |
| Entry Point | The main page is src/pages/Index.tsx. |
| UI Library | ALWAYS try to use the shadcn/ui library. |
| Styling | Always use Tailwind CSS for styling. |
| Icons | The lucide-react package is available for icons. |
Sources: src/prompts/system_prompt.ts:366-386
Chat Modes and Prompts
The system prompt changes significantly based on the chatMode, tailoring the AI's persona and capabilities to the task at hand.
Sources: src/prompts/system_prompt.ts:508-520
Build Mode (build)
This is the primary mode for creating and modifying code. The AI acts as an "AI editor" that makes real-time changes to the user's application.
Key Instructions:
- Code Output: All code must be wrapped in
<dyad-write>tags. Markdown code blocks are strictly forbidden. Only one<dyad-write>block is allowed per file. - File Operations: Use specific
<dyad-*>tags for file manipulation. - App Commands: Suggest UI commands like
Rebuild,Restart, orRefreshusing the<dyad-command>tag. - Conciseness: Provide brief, non-technical summaries of changes.
- Completeness: Implement features fully; no partial implementations or placeholders.
| Dyad Tag | Purpose | Example |
|---|---|---|
<dyad-write> | Create or update a file. | <dyad-write path="src/App.tsx">...</dyad-write> |
<dyad-rename> | Rename a file. | <dyad-rename from="a.ts" to="b.ts"></dyad-rename> |
<dyad-delete> | Remove a file. | <dyad-delete path="src/old.ts"></dyad-delete> |
<dyad-add-dependency> | Install npm packages. | <dyad-add-dependency packages="react-hot-toast"></dyad-add-dependency> |
<dyad-command> | Suggest a UI command. | <dyad-command type="restart"></dyad-command> |
<dyad-chat-summary> | Set the chat title. | <dyad-chat-summary>Add new button</dyad-chat-summary> |
Sources: src/prompts/system_prompt.ts:71-260, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---default-build-mode-1.txt
Ask Mode (ask)
In this mode, the AI acts as a helpful assistant for web development but is strictly prohibited from writing any code. Its sole purpose is to provide explanations, guidance, and best practices.
Key Instructions:
- ABSOLUTE PRIMARY DIRECTIVE: You must not, under any circumstances, write or generate code.
- No
<dyad-*>tags: All tags for code generation or file manipulation are forbidden. - Conceptual Explanations: Focus on explaining concepts, patterns, and trade-offs using plain language, not code examples.
- Problem-Solving: Break down complex problems and explain the reasoning behind technical decisions.
Sources: src/prompts/system_prompt.ts:388-463, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
Agent Mode (agent)
This mode is for analyzing a development request and gathering necessary information before coding begins. The AI's role is to determine what tools, APIs, or external resources are needed.
Key Instructions:
- No Code Generation: Like Ask Mode, the AI must not write any code or provide implementation details.
- Tool Usage Framework: The prompt provides a decision framework for when to use tools (e.g., for external APIs, real-time data) and when not to (e.g., for simple, self-contained apps).
- Specific Output: If no tools are needed, the AI must respond with the exact phrase:
"Ok, looks like I don't need any tools, I can start building."
Sources: src/prompts/system_prompt.ts:465-519
Local Agent Mode (local-agent)
This mode empowers the AI to use a suite of tools to interact with the local filesystem and codebase directly. It is designed for more autonomous operations.
Key Instructions:
- Tool-Centric: The AI is expected to use tools for most actions.
- Best Practices: The prompt encourages reading files before writing, using
search_replacefor edits, and making surgical changes. - Autonomous Planning: The AI should create and execute a plan without waiting for user confirmation unless more information is required.
- Tool calls are used for actions like reading files, writing files, renaming, deleting, and adding dependencies.
Sources: src/prompts/system_prompt.ts:500-502, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
Specialized Prompts
Beyond the main chat modes, the system uses other specialized prompts for specific tasks.
Thinking Prompt
Before responding to a request, the AI is instructed to use <think></think> tags to outline its plan. This structured thinking process is intended to improve the accuracy and quality of the final response.
Required Structure:
- Use bullet points to break down steps.
- Bold key insights and important considerations.
- Follow a clear analytical framework (e.g., identify bug, examine code, diagnose causes, plan approach).
Sources: src/prompts/system_prompt.ts:8-69
Security Review Prompt
When a security review is requested, a specialized prompt is used to guide the AI. It acts as a security expert focused on identifying vulnerabilities that could lead to data breaches.
Key Instructions:
- Focus Areas: Authentication, Injection Attacks, API Security, and Client-Side Secrets.
- Output Format: Findings must be reported using a
<dyad-security-finding>tag with a specific structure including title, severity level, a plain-language explanation, risk assessment, potential solutions, and relevant files. - Severity Levels:
critical,high,medium,low.
Sources: e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
Conclusion
The system prompt architecture in Dyad is a sophisticated mechanism for controlling AI behavior. By using distinct prompts for different chat modes, injecting project-specific rules, and enforcing structured outputs, the system ensures that the AI operates as a reliable, context-aware, and effective assistant for a wide range of development tasks, from writing code to providing expert advice.