Code Generation & Modification
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
- src/prompts/summarize_chat_system_prompt.ts
- e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
- e2e-tests/snapshots/problems.spec.ts_problems-auto-fix---complex-delete-rename-write-1.txt
The Dyad application facilitates AI-driven code generation and modification through a structured system of prompts and custom XML-like tags. The AI, acting as a developer assistant, interprets user requests and produces responses containing these special tags. These tags are then parsed by the backend to perform actions on the user's filesystem, such as creating, updating, or deleting files, and managing project dependencies.
This system is governed by a comprehensive set of rules defined in system prompts, which vary depending on the AI's operational mode. These prompts ensure that the AI adheres to the project's tech stack, follows best practices, and performs actions in a predictable and controlled manner. The entire process is designed to be transparent, with the AI explaining its plan and summarizing the changes made.
Core Concepts
The code modification engine is built upon system prompts that guide the AI's behavior and a set of custom tags that represent concrete actions.
System Prompts & AI Modes
The AI's behavior is dictated by a system prompt constructed based on the selected chatMode. The constructSystemPrompt function assembles the final prompt by selecting a base prompt and injecting project-specific aiRules.
Sources: src/prompts/system_prompt.ts:413-424
The primary AI modes include:
| Mode | Description | Key Rule |
|---|---|---|
build | The default mode for actively creating and modifying code. The AI generates code using <dyad-*> tags. | Code generation is mandatory for fulfilling requests. |
ask | An explanatory mode where the AI answers questions and provides guidance without writing any code. | ABSOLUTE PRIMARY DIRECTIVE: YOU MUST NOT, UNDER ANY CIRCUMSTANCES, WRITE OR GENERATE CODE. |
agent | An information-gathering mode to analyze requests and determine necessary tools or resources before coding begins. | ABSOLUTELY NO CODE GENERATION. |
local-agent | A mode where the AI uses a set of tools (e.g., read_file, search_replace) to interact with the codebase more surgically. | Tool use is preferred over direct file writes. |
This diagram illustrates the selection of the system prompt based on the chat mode.
Sources: src/prompts/system_prompt.ts:413-446
The Thinking Process
Before generating a response, the AI is instructed to use a <think> block to outline its plan. This structured thinking process ensures all aspects of the user's request are considered before any code is generated.
The process involves:
- Identifying the user's specific request.
- Examining relevant components in the codebase.
- Diagnosing potential causes or planning the implementation.
- Formulating a step-by-step plan for code changes.
- Considering improvements beyond the immediate fix.
Sources: src/prompts/system_prompt.ts:8-66
Dyad Action Tags
The AI uses a specific set of XML-like tags to instruct the system to perform actions. These tags are the primary mechanism for all code and file modifications.
| Tag | Attributes | Description |
|---|---|---|
<dyad-write> | path, description | Creates a new file or completely overwrites an existing file at the specified path. The AI is instructed to always write the entire file content. |
<dyad-rename> | from, to | Renames a file or directory. |
<dyad-delete> | path | Deletes a file at the specified path. |
<dyad-add-dependency> | packages | Installs one or more npm packages. Package names are space-separated. |
<dyad-command> | type | Suggests a UI command for the user to execute. Valid types are rebuild, restart, and refresh. |
<dyad-chat-summary> | Sets a concise summary for the current chat session, which is used as the chat title. | |
<dyad-security-finding> | title, level | Used in security reviews to report a vulnerability with a title and severity level (critical, high, medium, low). |
<dyad-add-integration> | provider | Shows a button that allows the user to add an integration, such as Supabase. |
Sources: src/prompts/system_prompt.ts:88-112, src/prompts/summarize_chat_system_prompt.ts:31-34, 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
Code Generation and Modification Flow
The following diagram shows the end-to-end flow from a user's request to the final code modification on the file system.
This flow is derived from the instructions in the system prompts, which detail how the AI should format its response and the purpose of each tag.
Sources: src/prompts/system_prompt.ts:100-103, e2e-tests/snapshots/problems.spec.ts_problems-auto-fix---complex-delete-rename-write-1.txt
Example: Renaming and Deleting Files
The system prompt provides a clear example of how file operations are combined in a single response.
- A file is renamed using
<dyad-rename>. - The content of the newly renamed file is updated with
<dyad-write>. - An unrelated, unused file is removed with
<dyad-delete>. - Another file that imported the renamed component is updated to reflect the new path.
<dyad-rename from="src/components/UserProfile.tsx" to="src/components/ProfileCard.tsx"></dyad-rename>
<dyad-write path="src/components/ProfileCard.tsx" description="Updating the ProfileCard component...">
...
</dyad-write>
<dyad-delete path="src/components/Analytics.tsx"></dyad-delete>
<dyad-write path="src/pages/Dashboard.tsx" description="Updating any imports...">
...
</dyad-write>
<dyad-chat-summary>Renaming profile file</dyad-chat-summary>
Sources: src/prompts/system_prompt.ts:231-286
Chat Summarization
At the end of a response that includes code changes, the AI must include a <dyad-chat-summary> tag. The content of this tag is a very brief, non-technical sentence that summarizes the actions taken. This is used to title the chat session for future reference.
The SUMMARIZE_CHAT_SYSTEM_PROMPT provides specific instructions for generating a more detailed summary, including major changes and relevant files, but the primary output for the chat title comes from the <dyad-chat-summary> tag.
Sources: src/prompts/summarize_chat_system_prompt.ts:1-34, src/prompts/system_prompt.ts:94-95
Summary
The code generation and modification feature in Dyad is a robust system built on the interplay between detailed AI system prompts and a custom tag-based action language. By defining strict rules and operational modes, the system ensures that the AI assistant can reliably and safely modify a user's codebase in response to natural language requests. The use of a <think> block for planning and a <dyad-chat-summary> for titling ensures a transparent and organized user experience.