When talking to the AI assistant, you can specify the Trae The content in your AI Assistant (including code, files, folders, and workspaces) serves as context for the AI Assistant to read and understand, ensuring that the AI Assistant's responses are more in line with your requirements. This article describes three ways to specify context.
Way 1: Use the content inside the editor as context
The AI Assistant sees the current file by default when there is a code file being edited in your editor. You can ask the AI Assistant questions directly about the current file. If you want to ask a question about a section of code in a file, use the following steps:
- Check the code.
- Click on the hover menu of the Add to dialog button to add the selection as a context to the side dialog.
The specified context is displayed in the input box at the bottom of the side dialog. In the following example, the input box shows the name of the file to which the selected content belongs, and the selected code line number.
- (Optional) Continue to add additional content snippets from the editor, or context from other sources at the same time.
- Next to Context, enter your question and send it to the AI assistant.
Approach 2: Use the contents of the terminal as the context
If you wish to ask questions about the output in the terminal (e.g., to help you fix reported errors), use the following steps:
- In the terminal, click Output Content Fragment.
- In the upper right corner of the content snippet area, click the Add to dialog button to add the selection as a context to the side dialog.
The specified context is displayed in the input box at the bottom of the side dialog. In the example below, the input box shows the source of the context and the line number.
- (Optional) Continue to add additional content snippets from the terminal, or context from other sources at the same time.
- Next to Context, enter your question and send it to the AI assistant.
Way 3: Use the # key to add context
In the input box of the side dialog, you can add multiple types of contexts, including code, files, folders and workspaces, using the # symbol. Typically, the list will show something related to the currently open file in the editor as the recommended context, but you can still search for the desired context and add it to the input box on your own. Based on your problem, you can add related contexts from various sources in combination (e.g. code and files at the same time).
#Code
With #Code, you can use the code associated with a function or class as a context for conversations with the AI assistant. The list defaults to showing the function or class in the file currently open in the editor. Before selecting, you can preview the code associated with the function or class recommended in the list. If the recommendation is not what you need, you can search for the function or class by keyword.
If the LSP for the corresponding language does not exist in Trae, please install it in advance, otherwise the code symbols may not be recognized.
- In the input box, type # , or just click on the input box in the lower left corner of the # Quote Button.
A list of context type choices is displayed above the input box.
- Select in the list Code (or in # Symbol followed by manual entry Code ), and then press the Enter key.
The list will show you the functions and classes present in the currently opened file in the editor. After hovering your mouse over an entry in the list, the code content of that function or class will be displayed on the left side for you to preview.
- If the recommended functions and classes are not what you need, add a new class to the #Code. After that, enter the name or keyword of the function or class you want.
The AI Assistant will search for relevant functions and classes within the project and display them in a list that you can preview.
- Select the function or class to be specified as a context from the list.
- After the code identifier in the input box, type your question and send it.
#File
With #File, you can use everything in a given file as a context for conversations with the AI assistant. By default, the list shows the files that have been recently opened in the editor. You can preview the directory where these files are located so that you don't make a mistake by selecting a file with the same name. If the displayed file is not what you need, you can search for it by keyword.
- In the input box, type # , or just click on the input box in the lower left corner of the # Quote Button.
A list of context type choices is displayed above the input box.
- Select in the list File (or in # Symbol followed by manual entry File ), and then press the Enter key.
The list shows the files that have been recently opened in the editor. After hovering the mouse over an entry in the list, the directory where the file is located is displayed on the left.
- If the recommended file is not what you need, there will be a new file in the #File. After that, enter the name or keyword of the file you want.
The AI Assistant will search for relevant files within the project and display them in a list.
- Select the file to be specified as the context from the list.
- After the file identification in the input box, type your question and send it.
#Folder
With #Folder, you can use all the contents of a specified folder as the context for a conversation with the AI assistant. The list shows by default the folders related to the currently opened file in the editor. You can preview the directory where these folders are located to avoid wrong selection due to the existence of folders with the same name. If a folder is displayed that you don't want, you can search for it by keyword.
#Folder relies on the Project Code Index being fully constructed. If the index is not fully constructed yet, it may affect the folder content recall, resulting in incomplete answers from the AI assistant. When you ask a question, if the index has never been built or is in the process of being built, the References location will appear with the message "Index is being built" or "Index is not being built yet", and you can follow the guidance to manage the index on the Settings page.
- In the input box, type # , or just click on the input box in the lower left corner of the # Quote Button.
A list of context type choices is displayed above the input box.
- Select in the list Folder (or in # Symbol followed by manual entry Folder ), and then press the Enter key.
The list displays the folder associated with the file currently open in the editor. After hovering the mouse over an entry in the list, the directory in which the folder is located is displayed on the left.
- If the recommended folder is not what you need, in the #Folder. After that, enter the name or keyword of the folder you want.
The AI Assistant will search for relevant folders within the project and display them in a list.
- Select the folder to be assigned as the context from the list.
- After the folder identification in the input box, type your question and send it.
#Workspace
You can use #Workspace to ask the AI Assistant questions about the entire workspace, and the AI Assistant will automatically find the most relevant content in the workspace to answer your questions in context. When you encounter an unfamiliar project, this feature helps you get a global view of the functionality and code implemented in the project, so you can get up to speed quickly.
Supported only in Chat mode. builder mode automatically uses the entire workspace as a context.
#Workspace relies on the Project Code Index being fully constructed. If the index is not fully built yet, it may affect the folder content recall, resulting in incomplete answers from the AI assistant. When you ask a question, if the index has never been built or is in the process of being built, the References location will show a prompt that says "Index is being built" or "Index is not being built yet", which you can use as a guide to manage the index on the Settings page.
- In the input box, type # , or just click on the input box in the lower left corner of the # Quote Button.
A list of context type choices is displayed above the input box.
- Select in the list Workspace (or in # Symbol followed by manual entry Workspace ).
- After the Workspace logo in the input box, type your question and send it.