Customizing Context with AI_RULES.md
Relevant source files
The following files were used as context for generating this wiki page:
- scaffold/AI_RULES.md
- e2e-tests/fixtures/import-app/minimal-with-ai-rules/AI_RULES.md
- src/ipc/utils/context_paths_utils.ts
- src/prompts/system_prompt.ts
- e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
- e2e-tests/snapshots/security_review.spec.ts_security-review---edit-and-use-knowledge-1.txt
The AI_RULES.md file is a crucial mechanism within the Dyad ecosystem for providing project-specific context and constraints to the AI assistant. By creating this file at the root of a project, developers can guide the AI's behavior, ensuring it adheres to the project's unique tech stack, architectural patterns, and coding conventions. This customization is essential for generating code that is consistent, maintainable, and aligned with the project's existing structure.
The rules defined in AI_RULES.md are dynamically loaded and injected into the system prompts for various AI modes, including build, ask, and local-agent. If this file is not present, the system falls back to a set of default rules, which are designed for a standard React/TypeScript application.
Core Functionality
The system is designed to be flexible, reading a user-defined rules file if it exists, and otherwise using a sensible default configuration.
Sources: src/prompts/system_prompt.ts:668-682
Loading AI Rules
The readAiRules function is responsible for loading the content of AI_RULES.md. It looks for the file in the root of the user's application path (dyadAppPath). If the file is found, its contents are read and returned as a string. If the file cannot be read for any reason (e.g., it doesn't exist), the function logs the error and returns the DEFAULT_AI_RULES constant as a fallback.
Sources: src/prompts/system_prompt.ts:698-710
The following diagram illustrates the loading process:
This diagram shows the logic flow of the readAiRules function, including the fallback to default rules.
Sources: src/prompts/system_prompt.ts:698-710
Prompt Injection
Once the AI rules are loaded (either from the file or the default constant), the constructSystemPrompt function injects them into the main system prompt. The various system prompt templates (e.g., BUILD_SYSTEM_PROMPT, ASK_MODE_SYSTEM_PROMPT) contain a [[AI_RULES]] placeholder. This placeholder is replaced with the actual rules content before the prompt is sent to the AI model.
Sources: src/prompts/system_prompt.ts:430-434, src/prompts/system_prompt.ts:668-682, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt:90-107
This diagram shows how the final system prompt is assembled:
This diagram illustrates the assembly of the final system prompt by replacing the [[AI_RULES]] placeholder.
Sources: src/prompts/system_prompt.ts:430-434, src/prompts/system_prompt.ts:679-680
AI Rules in Different Chat Modes
The AI_RULES.md content is utilized across different chat modes to tailor the AI's responses to the specific task.
| Chat Mode | Purpose of AI_RULES | Source File |
|---|---|---|
build | Guides code generation, file structure, and library usage. | src/prompts/system_prompt.ts:432 |
ask | Provides context for answering technical questions and giving guidance. | src/prompts/system_prompt.ts:553 |
local-agent | Informs the agent about the project's tech stack for planning and tool usage. | src/prompts/system_prompt.ts:672 |
agent | Provides context for requirement analysis and tool selection. | src/prompts/system_prompt.ts:689-692 |
Sources: src/prompts/system_prompt.ts:668-696
Default AI Rules
When a custom AI_RULES.md is not provided, the system uses a default set of rules defined in the DEFAULT_AI_RULES constant. These defaults are geared towards a modern React application.
Sources: src/prompts/system_prompt.ts:436-459, src/prompts/system_prompt.ts:707-709
| Category | Rule |
|---|---|
| Tech Stack | You are building a React application. |
| Use TypeScript. | |
Use React Router. Keep routes in src/App.tsx. | |
| Use Tailwind CSS for all styling. | |
| File Structure | All source code must be in the src folder. |
Pages should be placed in src/pages/. | |
Components should be placed in src/components/. | |
The main page is src/pages/Index.tsx. | |
| Library Usage | Always try to use the shadcn/ui library. |
lucide-react is installed for icons. | |
All shadcn/ui and Radix UI components are pre-installed. | |
Do not edit pre-built shadcn/ui components; create new ones if changes are needed. |
Sources: src/prompts/system_prompt.ts:436-459, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt:90-107
Example AI_RULES.md File
The following is an example of AI_RULES.md content found in the project's test suite. It mirrors the default rules, demonstrating the expected format and content.
# 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: e2e-tests/snapshots/security_review.spec.ts_security-review---edit-and-use-knowledge-1.txt:104-126, e2e-tests/fixtures/import-app/minimal-with-ai-rules/AI_RULES.md
Conclusion
The AI_RULES.md file is a powerful feature for tailoring the Dyad AI to specific project requirements. By defining the technology stack, file organization, and preferred libraries, developers can ensure the AI generates code that is idiomatic to their codebase. The system's fallback to a comprehensive set of default rules ensures that the AI remains effective even without a custom configuration, while the dynamic injection mechanism provides a seamless way to apply these constraints across all AI interactions.