AI Personal Learning
and practical guidance
TRAE

Trae IDE Guide: Easy Configuration of Custom AI Rules (Trae Rules)

As the application of AI in programming becomes more and more in-depth, many developers, after experiencing the convenience of AI-assisted programming, expect to have a "dedicated AI engineer" that understands their needs better. At the core of this demand is the ability of AI to precisely follow the developer's personalized instructions and specific project specifications. To fulfill this need, the Trae The IDE introduces AI Rules (Trae Rules) configuration, which allows users to customize behaviors for AI to automate the execution of personalized tasks.

I. What are Rules

Trae Rules is a powerful feature that allows development teams or individual developers to customize and force AI to follow specific code styles and best practices when code is generated, interpreted, or modified.

Imagine a common scenario where a developer using an AI for programming assistance may need to emphasize the same instructions to the AI over and over again, such as the language style of the code, the framework constraints that the project must follow, the level of detail and formatting of comments, or the security compliance regulations that need to be adhered to. The Trae Rules feature pre-sets these behavioral norms for the AI so that the AI can automatically "read" and follow these pre-set rules every time it responds. In this way, developers don't need to be reminded repeatedly, and the AI can steadily output content that meets expectations. More importantly, when dealing with different development tasks or switching projects, the corresponding Trae Rules can help the AI accurately adapt to the technical requirements of the current work, thus significantly improving development efficiency. These rule files are usually presented in an easy-to-read and edit text or Markdown format (e.g. user_rules.md cap (a poem) project_rules.md) exists to facilitate version control and team sharing by developers.

II. Instructions for use

After updating Trae IDE to version 0.5.1 and above, users can configure two types of rules:

01 Personal rules

Personal rules are rules customized for the AI based on developers' personal habits and preferences, with the aim of making the AI's output more in line with the user's personalized needs. Once a personal rule is set, it will take effect in all of the user's projects. For example:

  • language: Specify whether the AI answers in a concise, rigorous, or humor-oriented manner.
  • Operating System Preferences: Require AI to provide solutions or code samples for a specific operating system, such as Windows or macOS.
  • Depth of content: Set whether the AI needs to provide detailed explanations, code examples, or just the core conclusions.
  • interaction method: For example, users may prefer direct answers to leading rhetorical questions.

02 Project rules

A project rule is a collection of rules that the AI is required to follow for the particular project at hand. These rules take effect only in the project where they are configured. Example:

  • coding style: Specify code indentation (e.g., whether to use spaces or tabs and how many), naming conventions (e.g., camel-crossing nomenclature) camelCase or underscore nomenclature snake_case), etc.
  • Language and framework constraints: Specify the preferred programming language (e.g. Python or JavaScript) or mandatory framework (e.g. React (or Django).
  • API Usage Limitations: AI is explicitly prohibited from using certain outdated or unrecommended APIs in generated code.

Typically, if there is a content conflict between the personal rules and the project rules, the project rules take precedence and will override the corresponding provisions in the personal rules to ensure compliance with the project specifications.


The following sections demonstrate how to set up each of these two AI rules:

Creating personal rules

  1. In the upper right corner of the AI dialog window in Trae IDE, click on the "Settings" icon and select the "Rules" option.
     
    After the operation, the interface will display the "Rules" configuration window.
  2. In the Personal Rules area, click on + 创建 user_rules.md Button.
    The system automatically creates user_rules.md file and open the file in the editor.
  3. open user_rules.md file, enter the rules you want the AI to follow in natural language. For example, you can specify, "Please use Chinese for all code comments and explain the core function and parameter meanings of each function."
  4. Save the file. These rules then take effect for all project AI behaviors.

Creating Project Rules

  1. First, open a project in the Trae IDE.
  2. In the upper right corner of the AI dialog window, click the Settings icon and select Rules.
    After the operation, the interface will display the "Rules" configuration window.
  3. In the Project Rules area, click the + 创建 project_rules.md Button.

    The system automatically creates the current project root directory in the .trae/rules folder (if it doesn't already exist) and generate the project_rules.md Documentation.
  4. open project_rules.md Enter the rules to be followed by the AI for this project in the file.
  5. Save the file. These rules will only apply to the AI behavior of the current project.

Administrative rules

In the "Rules" configuration window, users can easily modify existing personal rules and project rules, and can also directly delete rule files that are no longer needed.

  • Modify Rules: After editing and saving the contents of the rules file, the AI will follow the updated rules.
  • Delete rules file: Delete user_rules.md maybe project_rules.md file, all rules previously defined in it will be invalidated.

III. Practical demonstration

Next, we'll walk you through a concrete, real-world example of how to use Trae Rules to improve development efficiency.

Suppose a development team is working on a web project and decides to use the Chakra UI framework to optimize its CSS styles. To ensure that the AI follows this decision in subsequent work, you can add the project rules (project_rules.md) file by adding the following directive:

请使用 Chakra UI 框架对本项目中的所有前端页面进行 CSS 相关的优化和重构。确保生成的组件符合 Chakra UI 的设计规范和最佳实践。

When this project rule is configured, it will be automatically referenced by the AI (which may be called Builder or a similar moniker in the Trae IDE) when a developer requests it for CSS-related development tasks.

For example, if another personal rule is configured, such as, "Code explanations must be thorough and explain key steps," then the AI will not only use the framework when performing the Chakra UI modification task described above, but will also provide more detailed information when explaining its operation.

IV. Application scenarios

Trae Rules can be used in a wide range of scenarios to help development teams and individual developers maintain code consistency, improve code maintainability, and reduce the occurrence of low-level errors in a variety of situations. Below is a list of some of the major scenarios:

Scenario 1: Teamwork and Code Review

  • effect: Harmonize code styles, reduce review disputes, and improve collaboration efficiency.
  • clarificationNew team members often need time to adapt to the team's established code style. By setting up project rules that specify things like variable naming conventions, module import order, and team-advocated code architectural patterns, the AI follows these rules when assisting with programming, ensuring that new team members are submitting code that is consistent with the team's style. In addition, Trae Rules can assist team members in formatting code, automatically fixing non-compliant code snippets, and enforcing comment specifications (e.g., requiring JSDoc-style comments for all publicly available functions) to reduce the cost of communicating stylistic issues during the code review phase.

Scenario 2: Project Maintenance and Code Quality

  • effect: Assist in detecting potential code errors, incrementally improving old code, and improving overall project maintainability.
  • clarification: For some legacy projects with a long history and a more chaotic code style, old code can be fixed in phases by gradually introducing and refining Trae Rules and combining them with conversational commands from the AI, which can identify code that doesn't conform to the new specification based on the rules and provide refactoring suggestions. At the same time, to prevent the introduction of new low-level errors during development, rules can be set, such as mandating asynchronous operations await must try-catch Structure wrapping, detection of unused import statements or variables, prohibiting the use of error-prone == and enforce the use of === Make comparisons. These rules help to minimize potential runtime problems and improve the robustness and long-term maintainability of the code.

Scenario 3: Specific Technology Stack Optimization

  • effect: Enhance the application of best practices for specific technology stacks (e.g. React, Vue, Node.js).
  • clarification: In front-end projects, inconsistencies in the way components are written can make them harder to understand and maintain. You can enforce a consistent way of writing components by setting up Trae Rules, for example by promoting the use of functional components in React projects (prohibiting the use of class component misuse), requiring that all components of the props must be type-checked (whether using TypeScript or PropTypes), and the mandatory useEffect Hook dependencies must be declared in full. For back-end Node.js projects with inconsistent API interface styles, rules can be passed to enforce compliance with the path format specification for RESTful APIs, require that the error handling process must be standardized, and prohibit the use of the console.log, switching to mandatory use of a unified logging repository.

Scenario 4: Security and Compliance

  • effect: Aids in preventing common security breaches and meeting industry-specific compliance requirements.
  • clarificationCode security is the cornerstone of project success. For common security risks, such as SQL injection, cross-site scripting attacks (XSS), etc., Trae Rules can set rules to help prevent them. For example, prohibit splicing SQL query strings directly into code (forcing the use of parameterized queries or ORM), detecting direct output to HTML without sufficiently escaping user input (to prevent XSS attacks), and prohibiting the use of eval() maybe new Function() Trae Rules can assist in detecting the inclusion of sensitive information in plaintext (e.g. passwords and keys) in code. For industries with stringent data protection and compliance auditing requirements, such as finance and healthcare, Trae Rules can assist in detecting whether sensitive information (e.g., passwords, keys) is contained in plaintext in the code, forcing encrypted storage of sensitive data such as passwords, and ensuring that logging does not contain Personally Identifiable Information (PII), thus helping projects to meet the needs of compliance checks.

Scenario 5: Optimization of Personal Development Habits

  • effect: Help developers maintain consistency in their personal coding style and learn and apply the features of new programming languages more efficiently.
  • clarification: Even for personal projects, maintaining a consistent code style can help improve development efficiency and code readability. Developers can do this by setting personal rules. Additionally, when learning a new programming language (e.g., Rust, Go, Python), it's easy for beginners to write code that doesn't conform to the language's usual paradigms or best practices. By setting Trae Rules, for example in Rust development, that enforce the use of the Result Types are handled properly (direct calls are prohibited) unwrap() (ignoring errors), forcing the checking and handling of function returns in Go development, the err variables (which prohibits ignoring errors), or forcing the use of type annotations in Python development and ensuring that they are consistent with the mypy Type-checking tools, such as the compatible, can effectively help developers to develop good coding habits and grasp the essence of the new language faster.

By carefully setting and applying Trae Rules, AI can be more accurately adapted to the specific needs and work habits of developers, and more seamlessly integrated into the daily development workflow, truly realizing efficient human-computer collaboration. Interested developers should download the latest version of Trae IDE from the official Trae IDE channel to experience this feature for themselves.

May not be reproduced without permission:Chief AI Sharing Circle " Trae IDE Guide: Easy Configuration of Custom AI Rules (Trae Rules)
en_USEnglish