Building from Source
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/chat_mode.spec.ts_chat-mode-selector---default-build-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/turbo_edits_v2.spec.ts_turbo-edits-v2---search-replace-dump-2.txt
The System Prompt Architecture is a modular system responsible for dynamically constructing the master prompt sent to the AI model. It defines the AI's role, capabilities, constraints, and output format based on the user's selected "chat mode." This ensures the AI behaves appropriately for different tasks, such as writing code, answering questions, or performing security analysis.
The core of this architecture is the constructSystemPrompt function, which assembles the final prompt from various templates, rules, and conditional blocks. This allows for a high degree of control over the AI's behavior without modifying the core model interaction logic.
Core Logic and Flow
The primary function, constructSystemPrompt, orchestrates the assembly of the final system prompt. Its logic is determined by the specified chatMode.
This diagram illustrates the high-level flow of the constructSystemPrompt function.
Sources: src/prompts/system_prompt.ts:608-625
Key Functions
| Function | Description |
|---|---|
constructSystemPrompt | The main entry point. It takes aiRules, chatMode, and enableTurboEditsV2 to build and return the final system prompt string. |
getSystemPromptForChatMode | A selector function that returns the appropriate base prompt template (BUILD_SYSTEM_PROMPT, ASK_MODE_SYSTEM_PROMPT, etc.) based on the chatMode. |
readAiRules | Asynchronously reads the AI_RULES.md file from the project directory. If the file doesn't exist, it returns the DEFAULT_AI_RULES constant as a fallback. |
Sources: src/prompts/system_prompt.ts:608-648
Chat Modes & Prompts
The system supports several distinct chat modes, each with a specialized system prompt to guide the AI's behavior.
| Mode | Purpose | Key Prompt Variable |
|---|---|---|
build | The AI acts as a software developer, writing and modifying code using special <dyad-*> tags. This is the default mode. | BUILD_SYSTEM_PROMPT |
ask | The AI acts as a helpful assistant, providing explanations and guidance without writing any code. | ASK_MODE_SYSTEM_PROMPT |
agent | The AI acts as a planning agent to analyze requests and determine necessary tools or resources before coding begins. It does not generate code. | AGENT_MODE_SYSTEM_PROMPT |
local-agent | A mode for local development that uses tool-calling conventions to interact with the file system and manage dependencies. | constructLocalAgentPrompt |
security-review | A special mode where the AI acts as a security expert, identifying vulnerabilities and formatting them in <dyad-security-finding> tags. | (Not a standard chat mode, but a distinct prompt) |
Sources: src/prompts/system_prompt.ts:613-616, src/prompts/system_prompt.ts:627-642
Build Mode
This is the primary mode for code generation. The AI is instructed to act as an "AI editor that creates and modifies web applications."
Key Directives:
- Use
<dyad-*>XML tags to perform actions. - Do not suggest shell commands; use
<dyad-command>instead. - Write complete, functional code. No placeholders or partial implementations.
- Create new files for new components.
- Do not use markdown code blocks for code output.
Action Tags:
| Tag | Description |
|---|---|
<dyad-write path="..." description="..."> | Creates or overwrites a file with the provided content. |
<dyad-rename from="..." to="..."> | Renames a file. |
<dyad-delete path="..."> | Deletes a file. |
<dyad-add-dependency packages="..."> | Adds one or more npm packages (space-separated). |
<dyad-command type="..."> | Suggests a UI command (rebuild, restart, refresh). |
<dyad-chat-summary> | Sets a concise summary for the chat history. |
Sources: src/prompts/system_prompt.ts:71-140, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---default-build-mode-1.txt
Turbo Edits V2 (dyad-search-replace)
When enableTurboEditsV2 is true, the TURBO_EDITS_V2_SYSTEM_PROMPT is appended to the BUILD_SYSTEM_PROMPT. This enables the use of the <dyad-search-replace> tag for making surgical edits to existing files, which is preferred over rewriting the entire file with <dyad-write>.
Sources: src/prompts/system_prompt.ts:640-642, e2e-tests/snapshots/turbo_edits_v2.spec.ts_turbo-edits-v2---search-replace-dump-2.txt
Ask Mode
In "Ask Mode," the AI's primary directive is to be a helpful, educational assistant that never produces code.
Key Principles:
- NO CODE PRODUCTION: An absolute prohibition on writing code, code snippets, or syntax examples.
- Clarity First: Prioritize clear communication through conceptual explanations.
- Best Practices: Recommend industry-standard approaches through descriptions.
- Forbidden Tags: All
<dyad-*>tags are strictly forbidden.
The prompt explicitly states: "YOU MUST NOT, UNDER ANY CIRCUMSTANCES, WRITE OR GENERATE CODE."
Sources: src/prompts/system_prompt.ts:430-510, e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
Agent Mode
This mode is for pre-development analysis. The AI's role is to "analyze app development requests and gather all necessary information before the actual coding phase begins."
Key Directives:
- Determine if external tools, APIs, or data are needed.
- If no tools are needed, respond with a fixed phrase: "Ok, looks like I don't need any tools, I can start building."
- ABSOLUTELY NO CODE GENERATION or use of
<dyad-*>tags.
Sources: src/prompts/system_prompt.ts:512-573
Local Agent Mode
This mode is structured for tool-based interactions, common in local development agent setups. The prompt defines a clear structure for tool calling and best practices.
Prompt Structure:
<role>: Defines the AI as "Dyad, an AI assistant."<app_commands>: Defines UI commands likerebuild,restart.<general_guidelines>: General rules for interaction.<tool_calling>: Rules for how to use tools, make plans, and handle errors.<tool_calling_best_practices>: Guidelines like "Read before writing" and "Use search_replace for edits."
Sources: src/prompts/system_prompt.ts:611-613, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
Security Review Prompt
While not a standard chatMode, a distinct prompt is used for the /security-review command. It instructs the AI to act as a security expert.
Key Directives:
- Focus Areas: Authentication, Injection Attacks, API Security, and Client-Side Secrets.
- Output Format: Must use the
<dyad-security-finding>tag with a specific structure (title,level,What,Risk,Potential Solutions,Relevant Files). - Severity Levels: Defines
critical,high,medium, andlowseverities.
Sources: e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
AI Rules and Configuration
The AI_RULES.md file provides project-specific context about the tech stack and coding conventions. This file is loaded and injected into the system prompt to ensure the AI's output aligns with the project's standards.
The readAiRules function attempts to load AI_RULES.md. If it fails, it falls back to a set of default rules.
This diagram shows the logic for loading AI rules.
The default rules (DEFAULT_AI_RULES) specify a standard React/TypeScript/Tailwind stack and note the availability of lucide-react and shadcn/ui components.
Sources: src/prompts/system_prompt.ts:400-427, src/prompts/system_prompt.ts:644-655
Additional Prompt Components
Two other significant prompt blocks are defined for general use.
THINKING_PROMPT: This is a meta-instruction for the AI. It mandates that the AI use<think></think>tags to outline its plan in bullet points before generating the final response. This encourages a structured, analytical approach to problem-solving. Sources: src/prompts/system_prompt.ts:8-67BUILD_SYSTEM_POSTFIX: This block contains non-negotiable formatting rules, emphatically prohibiting the use of markdown code blocks (```) and mandating the use of<dyad-write>for all code output inbuildmode. Sources: src/prompts/system_prompt.ts:384-398