AI Request Context

Index of All Documentation » Wing Pro Reference Manual » AI Assisted Development »


Wing always sends at least some of the contents of your current editor to the AI provider as part of its completion, refactoring, and chat requests. At your discretion, other files may be sent as well as context for the request, so that results are better tailored to your existing code base. You can specify the amount and type of context sent with AI requests, and this can have considerable impact on the quality of the results that you receive.

Adding context can cause your requests to become hugely compute intensive and costly over time, so you will want to include additional context only when necessary. The more relevant context you provide, the better results you will obtain, but larger requests take longer to complete, cost significantly more, and may reach rate limits imposed by the AI provider. In some cases, providing irrelevant context may even have a negative impact on the quality of results that you receive.

In general, you should think about whether your request needs anything more than the contents of the current file and the AI's training set to be answered correctly. If you are asking the AI to write or refactor code using only code in the current file, then you do not need to include additional context. On the other hand, if your request needs to use imported custom modules that won't exist in the AI's training set, then you will get better results if you first add those imports to your file and enable auto-context for your request.

Auto-Context

For AI requests that would benefit from context found outside of the current editor file, you can enable auto-context and let Wing automatically find files that are likely to be relevant to your request.

For AI code suggestion, auto-context is enabled with AI Suggest Uses Auto-Context on the Config: Request Context page of the AI tool.

For AI refactoring and chat, auto-context may be enabled or disabled on a case by case basis from the AI Refactoring and AI Chat pages of the AI tool.

Each of these enables or disables auto-context independently from each other.

Auto-context adds other files to AI requests in the following order: (1) any project files that are imported into the current file, (2) any other project files that are currently open in an editor, and (3) any other files in the project that were not imported or open in an editor. Which of these are actually used and how much total context to include can be configured, as described in the section Configuration below.

In many cases, only a small subset of all possible files will be included with your request, because of the computationally intensive nature of generative AI. To increase the chances that the more relevant files are included, more recently used files are given priority. For imported files, the later imports in a file are used first, in the hope that they will contain more useful high-level context.

When this approach fails to select context accurately enough, you may receive less useful responses to your requests. In this case, you can explicitly include or exclude files from context, as described in the section Refining the Context File Set below.

Viewing Context

The files that are candidates for inclusion in auto-context change according to current focus. You can see which files are currently being included in auto-context by hovering the mouse over the Auto-Context checkbox in the AI refactoring and chat pages of the AI tool. AI suggest uses the same context as AI refactor. The set of files will change depending on which files are in the project, which editors are open, which editor is current, the content of the current editor, and the configuration settings described below.

AI Suggest and Refactor

For AI suggest and refactor, context is computed when the request is sent to the AI provider, and limited by the configured constraints on request size. In most cases, only a subset of files can be included. Wing builds this subset in a way that attempts to retain important code structure, while omitting the details of some parts of the implementation. If a subset of a file is being included as context for AI suggest and refactor, this is indicated in the tooltip shown over the Auto-Context checkbox on the AI Refactor page of the AI tool, and in the popup menu next to it.

AI Chat

For AI chat, Wing instead uploads whole files to the AI provider ahead of time, to act as context for your conversation with the AI assistant. The upload status of files is indicated by the icon to the right of the Auto-Context checkbox on the AI Chat page of the AI tool. You will see this indicator change color and animate whenever you change focus, if you add or remove imports in a file, or if you take any other action that changes the set of files in auto-context.

If the icon is not green, then the AI assistant does not have access to all the context files and you may receive results that reflect their omission. You can see which files have been uploaded, which are still being uploaded, and which failed to upload in the tooltip over the Auto-Context checkbox or in the contents of the popup menu on the icon next to it.

The icons used are:

waiting -- An animated version of this icon indicates the context files are being uploaded.

ready -- Indicates that all context files have been uploaded and the AI assistant is ready for requests.

warning -- Indicates that one or more files failed to upload but requests can still be made to the AI assistant.

error -- Indicates that a fatal error is preventing requests to the AI assistant. Use Show Console in the Options menu to diagnose the problem.

disallowed -- Indicates that you have not given Wing permission to upload any data to an AI provider. In this case the AI features are entirely disabled. You will be prompted to allow uploads if you try to use any of them.

Refining the Context File Set

By default Wing automatically adds as many context files as it can within the configured constraints on context size, in the order described earlier. You can modify this behavior by clicking on the icon to the right of the Auto-Context checkbox on the AI refactoring and AI chat pages of the AI tool. This pops up a menu that shows all the files that are candidates for inclusion as context.

The menu indicates automatically included files with a small green dot autoinclude next to the file name. Files that are candidates for inclusion but that exceeded the constraints on context size are shown further down the list, without any icon next to them.

If you select a menu item that is currently automatically being included in request context, that file will be blocked from being used subsequently in context. This is a good way to exclude large files or those that may not add much value to your requests. Files blocked from inclusion in request context are indicated with a red "do not enter" icon excluded. This action may free up some space in the available context, and thus allow selection of another file for automatic inclusion in request context. The menu's icons will update accordingly.

Selecting an item that is not currently included in the request context will explicitly add it to the context, and remove the lowest priority automatically added file. Explicitly selected context files are marked with a green check mark included. If too many items are selected for explicit inclusion, then some may not be able to be included after maximum context size is reached. Those files will be indicated in the menu with a yellow or orange check mark surplus.

Configuration

You can configure AI request context by opening the AI tool from the Tools menu and selecting Config: Request Context from the drop down at the top of the tool. The following options are available for controlling the size of request context for AI suggest and refactor:

  • Max Local Context (KB) sets the maximum number of kilobytes of code from the current editor that may be sent as context for any AI request. Up to this amount of context is always sent with every request.
  • Max Context Size (KB) sets the maximum size in kilobytes of all context sent with each AI request. This includes context from the current file, any files specified with Context Files (see below) and files identified as relevant when auto-context is enabled. This value is only used in AI suggest and refactor requests, since AI chat preloads most context and doesn't submit it with each request.
  • Max Preload Context (KB) sets the maximum size in kilobytes context that may be preloaded or preprocessed by uploading auto-context files to the AI provider. This value is only used in AI chat, since AI suggest and refactor send context with each request rather than uploading it.
  • Max Context Files sets the maximum number of files to include in context for AI requests. When this limit is reached, no more files will be added, even if their total size is still below the above thresholds.

The maximum sizes to use here are governed by the specific rate limits imposed by your AI provider account, and the cost of requests. Note, however, that the upper limit may never be reached if your files are small and/or you have not enabled auto-context or specified any custom context files.

It may take some experimentation to find the best value to use, where enough context is provided for quality results without overwhelming the AI provider or running into your account's rate limits.

You can also configure which files are included in auto-context with the following options:

  • Auto-Context: Imported Project Files specifies whether imported project files should be included as request context, when auto-context is enabled. This is enabled by default.
  • Auto-Context: Open Project Files specifies whether open project files that were not imported should be included as request context, when auto-context is enabled. This is disabled by default.
  • Auto-Context: Other Project Files specifies whether other project files that were not imported and are not open in an editor should be included as request context, when auto-context is enabled. This is disabled by default.
  • Context Files allows you to specify files to always include as context with all AI requests, even when auto-context is disabled. These files are prioritized over any files added by auto-context, so that they will always be included, as long as they don't themselves exceed the configured maximum external context size or file count.

Finally, you can control whether AI code suggestion uses auto-context:

  • AI Suggest Uses Auto-Context controls whether Wing will automatically add additional files, other than the current file, as context when you are invoking AI code suggestion for the current insertion point in the editor. The context that is used is the same as for AI refactoring. Use the icon to the right of the Auto-Context check box on the AI Refactoring page of the AI tool to view and adjust auto-context for AI suggestion.

Using auto-context for AI suggestion is disabled by default since no additional context is needed for most AI code suggestion tasks. You will want to enable it only if your request needs access to the content of files other than the current one, and in that case should first import the files you want to use in the AI's implementation.