Skip to content

Commit e41993f

Browse files
authored
Chat response output types (#7632)
* Add supported response output types * Add bookmark * Change table to bulleted list * Add command links * Add command link example * Update after peer review * Fix path and remove leftover note
1 parent a508658 commit e41993f

File tree

1 file changed

+135
-11
lines changed

1 file changed

+135
-11
lines changed

api/extension-guides/chat.md

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ MetaDescription: A guide to creating an AI extension in Visual Studio Code
1111

1212
Visual Studio Code's Copilot Chat architecture enables extension authors to integrate with the [GitHub Copilot Chat](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat) experience. A chat extension is a VS Code extension that uses the Chat extension API by contributing a *Chat participant*.
1313

14-
1514
Chat participants are domain experts that can answer user queries within a specific domain. Participants can use different approaches to process a user query:
1615

1716
- Use AI to interpret the request and generate a response, for example by using the [Language Model API](/api/extension-guides/language-model)
@@ -20,13 +19,7 @@ Chat participants are domain experts that can answer user queries within a speci
2019

2120
Participants can use the language model in a wide range of ways. Some participants only make use of the language model to get answers to custom prompts, for example the [sample chat participant](https://github.com/microsoft/vscode-extension-samples/tree/main/chat-sample). Other participants are more advanced and act like [autonomous agents](https://learn.microsoft.com/semantic-kernel/agents/) that invoke multiple tools with the help of the language model. An example of such an advanced participant is the built-in `@workspace` that knows about your workspace and can answer questions about it. Internally, `@workspace` is powered by multiple tools: GitHub's knowledge graph, combined with semantic search, local code indexes, and VS Code's language services.
2221

23-
When a user explicitly mentions a `@participant` in their chat prompt, that prompt is forwarded to the extension that contributed that specific chat participant. The participant then uses a `ResponseStream` to respond to the request. To provide a smooth user experience, the Chat API is streaming-based. A `ResponseStream` can include:
24-
25-
- Markdown for simple text and image responses
26-
- Buttons that invoke VS Code commands
27-
- Progress for longer running operations
28-
- References to URIs or editor locations
29-
- File trees (for example, to show a workspace preview when a chat participant proposes to create a new workspace)
22+
When a user explicitly mentions a `@participant` in their chat prompt, that prompt is forwarded to the extension that contributed that specific chat participant. The participant then uses a `ResponseStream` to respond to the request. To provide a smooth user experience, the Chat API is streaming-based. A chat response can contain rich content, such as Markdown, file trees, command buttons, and more. Get more info about the [supported response output types](#supported-chat-response-output-types).
3023

3124
To help the user take the conversation further, participants can provide *follow-ups* for each response. Follow-up questions are suggestions that are presented in the chat user interface and might give the user inspiration about the chat extension's capabilities.
3225

@@ -36,7 +29,6 @@ Participants can also contribute *commands*, which are a shorthand notation for
3629

3730
Alternatively, it is possible to extend GitHub Copilot by creating a GitHub App that contributes a chat participant in the Chat view. A GitHub App is backed by a service and works across all GitHub Copilot surfaces, such as github.com, Visual Studio, or VS Code. On the other hand, GitHub Apps do not have full access to the VS Code API. To learn more about extending GitHub Copilot through a GitHub App see the [GitHub documentation](https://docs.github.com/en/copilot/building-copilot-extensions/about-building-copilot-extensions).
3831

39-
4032
## Links
4133

4234
- [Chat extension sample](https://github.com/microsoft/vscode-extension-samples/tree/main/chat-sample)
@@ -96,7 +88,7 @@ The first step to create a chat extension is to register it in your `package.jso
9688
}
9789
```
9890

99-
We suggest to use a lowercase `name` to align with existing chat participants. `name` can not contain spaces. Users can then reference the chat participant in the Chat view by using the `@` symbol and the `name` you provided. We suggest to use title case for the `fullName`, which is shown in the title area of a response from your participant. Some participant names are reserved, and in case you use a reserved name VS Code will display the fully qualified name of your participant (including the extension id). The `description` is shown in the chat input field as a placeholder text.
91+
We suggest using a lowercase `name` to align with existing chat participants. `name` can not contain spaces. Users can then reference the chat participant in the Chat view by using the `@` symbol and the `name` you provided. We suggest using title case for the `fullName`, which is shown in the title area of a response from your participant. Some participant names are reserved, and in case you use a reserved name VS Code will display the fully qualified name of your participant (including the extension ID). The `description` is shown in the chat input field as a placeholder text.
10092

10193
The `isSticky` property controls whether the chat participant is persistent, which means that the participant name is automatically prepended in the chat input field after the user has started interacting with the participant.
10294

@@ -202,6 +194,8 @@ stream.button({
202194
});
203195
```
204196

197+
Get more info about the [supported chat response output types](#supported-chat-response-output-types).
198+
205199
In practice, extensions typically send a request to the language model. Once they get a response from the language model, they might further process it, and decide if they should stream anything back to the user. The VS Code Chat API is streaming-based, and is compatible with the streaming [Language Model API](/api/extension-guides/language-model). This allows extensions to report progress and results continuously with the goal of having a smooth user experience. Learn how you can use the [Language Model API](/api/extension-guides/language-model).
206200

207201
#### Use the chat message history
@@ -269,11 +263,141 @@ cat.followupProvider = {
269263

270264
> **Tip:** Follow-ups should be written as questions or directions, not just concise commands.
271265
266+
## Supported chat response output types
267+
268+
To return a response to a chat request, you use the [`ChatResponseStream`](/api/references/vscode-api#ChatResponseStream) parameter on the [`ChatRequestHandler`](/api/references/vscode-api#ChatRequestHandler).
269+
270+
The following list provides the output types for a chat response in the Chat view. A chat response can combine multiple different output types.
271+
272+
- **Markdown**
273+
274+
Render a fragment of Markdown text simple text or images. You can use any Markdown syntax that is part of the [CommonMark](https://commonmark.org/) specification. Use the [`ChatResponseStream.markdown`](/api/references/vscode-api#ChatResponseStream.markdown) method and provide the Markdown text.
275+
276+
Example code snippet:
277+
278+
```typescript
279+
// Render Markdown text
280+
stream.markdown('# This is a title \n');
281+
stream.markdown('This is stylized text that uses _italics_ and **bold**. ');
282+
stream.markdown('This is a [link](https://code.visualstudio.com).\n\n');
283+
stream.markdown('![VS Code](https://code.visualstudio.com/assets/favicon.ico)');
284+
```
285+
286+
- **Code block**
287+
288+
Render a code block that supports IntelliSense, code formatting, and interactive controls to apply the code to the active editor. To show a code block, use the [`ChatResponseStream.markdown`](/api/references/vscode-api#ChatResponseStream.markdown) method and apply the Markdown syntax for code blocks (using backticks).
289+
290+
Example code snippet:
291+
292+
```typescript
293+
// Render a code block that enables users to interact with
294+
stream.markdown('```bash\n');
295+
stream.markdown('```ls -l\n');
296+
stream.markdown('```');
297+
```
298+
299+
- **Command link**
300+
301+
Render a link inline in the chat response that users can select to invoke a VS Code command. To show a command link, use the [`ChatResponseStream.markdown`](/api/references/vscode-api#ChatResponseStream.markdown) method and use the Markdown syntax for links `[link text](command:commandId)`, where you provide the command ID in the URL. For example, the following link opens the Command Palette: `[Command Palette](command:workbench.action.showCommands)`.
302+
303+
To protect against command injection when you load the Markdown text from a service, you have to use a [`vscode.MarkdownString`](/api/references/vscode-api#MarkdownString) object with the `isTrusted` property set to the list of trusted VS Code command IDs. This property is required to enable the command link to work. If the `isTrusted` property is not set or a command is not listed, the command link will not work.
304+
305+
Example code snippet:
306+
307+
```typescript
308+
// Use command URIs to link to commands from Markdown
309+
let markdownCommandString: vscode.MarkdownString = new vscode.MarkdownString(`[Use cat names](command:${CAT_NAMES_COMMAND_ID})`);
310+
markdownCommandString.isTrusted = { enabledCommands: [ CAT_NAMES_COMMAND_ID ] };
311+
312+
stream.markdown(markdownCommandString);
313+
```
314+
315+
- **Command button**
316+
317+
Render a button that invokes a VS Code command. The command can be a built-in command or one that you define in your extension. Use the [`ChatResponseStream.button`](/api/references/vscode-api#ChatResponseStream.button) method and provide the button text and command ID.
318+
319+
Example code snippet:
320+
321+
```typescript
322+
// Render a button to trigger a VS Code command
323+
stream.button({
324+
command: 'my.command',
325+
title: vscode.l10n.t('Run my command')
326+
});
327+
```
328+
329+
- **File tree**
330+
331+
Render a file tree control that lets users preview individual files. For example, to show a workspace preview when proposing to create a new workspace. Use the [`ChatResponseStream.filetree`](/api/references/vscode-api#ChatResponseStream.filetree) method and provide an array of file tree elements and the base location (folder) of the files.
332+
333+
Example code snippet:
334+
335+
```typescript
336+
// Create a file tree instance
337+
var tree: vscode.ChatResponseFileTree[] = [
338+
{ name: 'myworkspace', children: [
339+
{ name: 'README.md' },
340+
{ name: 'app.js' },
341+
{ name: 'package.json' }
342+
]}
343+
];
344+
345+
// Render the file tree control at a base location
346+
stream.filetree(tree, baseLocation);
347+
```
348+
349+
- **Progress message**
350+
351+
Render a progress message during a long-running operation to provide the user with intermediate feedback. For example, to report the completion of each step in a multi-step operation. Use the [`ChatResponseStream.progress`](/api/references/vscode-api#ChatResponseStream.progress) method and provide the message.
352+
353+
Example code snippet:
354+
355+
```typescript
356+
// Render a progress message
357+
stream.progress('Connecting to the database.');
358+
```
359+
360+
- **Reference**
361+
362+
Add a reference for an external URL or editor location in the list references to indicate which information you use as context. Use the [`ChatResponseStream.reference`](/api/references/vscode-api#ChatResponseStream.reference) method and provide the reference location.
363+
364+
Example code snippet:
365+
366+
```typescript
367+
const fileUri: vscode.Uri = vscode.Uri.file('/path/to/workspace/app.js'); // On Windows, the path should be in the format of 'c:\\path\\to\\workspace\\app.js'
368+
const fileRange: vscode.Range = new vscode.Range(0, 0, 3, 0);
369+
const externalUri: vscode.Uri = vscode.Uri.parse('https://code.visualstudio.com');
370+
371+
// Add a reference to an entire file
372+
stream.reference(fileUri);
373+
374+
// Add a reference to a specific selection within a file
375+
stream.reference(new vscode.Location(fileUri, fileRange));
376+
377+
// Add a reference to an external URL
378+
stream.reference(externalUri);
379+
```
380+
381+
- **Inline reference**
382+
383+
Add an inline reference to a URI or editor location. Use the [`ChatResponseStream.anchor`](/api/references/vscode-api#ChatResponseStream.anchor) method and provide the anchor location and optional title. To reference a symbol (for example, a class or variable), you would use a location in an editor.
384+
385+
Example code snippet:
386+
387+
```typescript
388+
const symbolLocation: vscode.Uri = vscode.Uri.parse('location-to-a-symbol');
389+
390+
// Render an inline anchor to a symbol in the workspace
391+
stream.anchor(symbolLocation, 'MySymbol');
392+
```
393+
394+
> **Important**: Images and links are only available when they originate from a domain that is in the trusted domain list. Get more info about [link protection in VS Code](/docs/editor/editingevolved.md#outgoing-link-protection).
395+
272396
## Variables
273397

274398
> **Note:** The Variables API is still in a proposed state and we are actively working on it.
275399

276-
Chat extensions can also contribute chat *variables*, which provide context about the extension's domain. For example, a C++ extension might contribute a variable `#cpp` that would get resolved based on the state of the language service - what C++ version is being used and what C++ programming approach is preferred.
400+
Chat extensions can also contribute chat *variables*, which provide context about the extension's domain. For example, a C++ extension might contribute a variable `#cpp` that would get resolved based on the state of the language service - what C++ version is used and what C++ programming approach is preferred.
277401

278402
Users can refer to a chat variable in a prompt by using the `#` symbol. A variable is resolved by either the chat extension that contributed that variable, or by VS Code when it's a built-in variable (for example, `#file` or `#selection`). VS Code offers the list of registered variables upon typing the `#` symbol in the chat input.
279403

0 commit comments

Comments
 (0)