# GitHub Copilot Chat

> Integrate with GitHub Copilot Chat to run scripts through a chat participant and custom prompts in Visual Studio Code. Learn how to execute scripts, select models, leverage context, and enable more efficient AI-assisted coding workflows using chat-based interfaces.

GenAIScript integrates with [GitHub Copilot Chat](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat) by providing a **chat participant** that allows you to run scripts in the context of a chat conversation, and a **custom prompt** to generate GenAIScript more efficiently with Copilot Chat.

Tip

If you need to check your available premium request quota for GitHub Copilot, go to [Features](https://github.com/settings/copilot/features)

## `@genaiscript` chat participant

[Section titled “@genaiscript chat participant”](#genaiscript-chat-participant)

The `@genaiscript` [chat participant](https://code.visualstudio.com/api/extension-guides/chat#parts-of-the-chat-user-experience) lets your run scripts without the context of a [GitHub Copilot Chat](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat) conversation. This is useful for leverage existing scripts in an interactive chat session.

![A screenshot of the chat participant window.](/genaiscript/_astro/chat-participant.BsdSg1Yh_u2VpW.webp)

Note

This feature is only available in **Ask** and **Edit** modes.

### Choosing which script to run

[Section titled “Choosing which script to run”](#choosing-which-script-to-run)

The `/run` command expects a script id as the first argument (e.g., `/run poem`). The rest of the query is passed to the script as the `env.vars.question` variable.

```sh
@genaiscript /run summarize
```

If you omit the `/run` command, GenAIScript will look for a script named `copilotchat`. If it finds one, it will run it. Otherwise, it will ask you to pick a script from the list of available scripts.

```sh
@genaiscript add comments to the current editor
```

### Choosing the model

[Section titled “Choosing the model”](#choosing-the-model)

If your script does not specify a model, GenAIScript will prompt you to choose a model. You can specify which model to choose in the script configuration as well using the `github_copilot_chat` provider.

* current selected model: `github_copilot_chat:current`

```js
script({
  model: "github_copilot_chat:current",
});
```

* gpt-4o-mini: `github_copilot_chat:gpt-4o-mini`

```js
script({
  model: "github_copilot_chat:gpt-4o-mini",
});
```

When GenAIScript prompts you to choose a model, it will store your choices in the workspace settings under the

```json
{
  "genaiscript.languageChatModels": {
    "gpt-4o": "gpt-4o-2024-11-20"
  }
}
```

Note

**The GitHub Copilot Chat models are only available in Visual Studio Code.** They will not work from the [cli](/genaiscript/reference/cli) or [playground](/genaiscript/reference/playground) interfaces.

#### Model availability

[Section titled “Model availability”](#model-availability)

Not all models listed in the GitHub Copilot Chat user interface are available for 3rd party extensions. When GenAIScript tries to access a model that is not available, it will notify you but it does not have over your model access configuration.

### Context

[Section titled “Context”](#context)

The context selected by the user in Copilot Chat is converted to variables and passed to the script:

* the prompt content is passed in `env.vars.question`. The script id is removed in the case of `/run`.
* the current editor text is passed in `env.vars["copilot.editor"]`
* the current editor selection is passed in `env.vars["copilot.selection"]`
* all other file references are passed in `env.files`

#### Examples

[Section titled “Examples”](#examples)

* `mermaid` will generate a diagram from the user prompt.

mermaid.genai.mjs

```js
def("CODE", env.files);
$`Generate a class diagram using mermaid of the code symbols in the CODE.`;
```

* `websearcher` will search the web for the user prompt and use the file in context in the answer.

websearcher.genai.mjs

```js
const res = await retrieval.webSearch(env.vars.question);
def("QUESTION", env.vars.question);
def("WEB_SEARCH", res);
def("FILE", env.files, { ignoreEmpty: true });
$`Answer QUESTION using WEB_SEARCH and FILE.`;
```

* `dataanalyst` uses the Python code interpreter tools to resolve a data computation question.

dataanalyst.genai.mjs

```js
script({
  tools: [
    "fs_read_file",
    "python_code_interpreter_copy_files_to_container",
    "python_code_interpreter_read_file",
    "python_code_interpreter_run",
  ],
});
def(
  "DATA",
  env.files.map(({ filename }) => filename).join("\n"),
);
def("QUESTION", env.vars.question);


$`Run python code to answer the data analyst question
in QUESTION using the data in DATA.
Return the python code that was used to compute the answer.
`;
```

#### History

[Section titled “History”](#history)

The history of messages is passed in `env.vars["copilot.history"]`. It is an array of `HistoryMessageUser | HistoryMessageAssistant`:

```json
[
  {
    "role": "user",
    "content": "write a poem"
  },
  {
    "role": "assistant",
    "content": "I am an assistant"
  }
]
```

### Continued conversation

[Section titled “Continued conversation”](#continued-conversation)

You can use the `@genaiscript` chat to weave the execution of a script into an existing conversation or to continue the conversation with Copilot with the results of the script. The results of the script are placed back into the chat history and are available to any copilot later on.

* `@genaiscript /run tool` will run the `tool` script and place the results back into the chat history.
* `analyze the results` will continue the conversation with the results of the script.

### Default script[]()

[Section titled “Default script ”](#default-script)

The following script can used as a starter template to create the default script when the user does not use the `/run` command.

genaisrc/copilotchat.genai.mts

```ts
script({
  title: "Reasoning Agent",
  description:
    "A reasoning agent that can answer questions about files, git, github, documentation, web queries, video analysis.",
  model: "large",
  system: [
    // List of system components and tools available for the script
    "system",
    "system.assistant",
    "system.safety_harmful_content",
    "system.safety_jailbreak",
    "system.safety_protected_material",
    "system.tools",
    "system.files",
    "system.files_schema",
    "system.diagrams",
    "system.annotations",
    "system.git_info",
    "system.github_info",
    "system.safety_harmful_content",
    "system.safety_validate_harmful_content",
    "system.agent_fs",
    "system.agent_git",
    "system.agent_github",
    "system.agent_interpreter",
    "system.agent_docs",
    "system.agent_web",
    "system.agent_video",
    "system.agent_data",
    "system.vision_ask_images",
    "system.think",
  ],
  group: "mcp", // Group categorization for the script
  parameters: {
    question: {
      type: "string",
      description: "the user question",
    },
    "copilot.editor": {
      type: "string",
      description: "the content of the opened editor, if any",
      default: "",
    },
    "copilot.selection": {
      type: "string",
      description: "the content of the opened editor, if any",
      default: "",
    },
  },
  flexTokens: 20000, // Flexible token limit for the script
});


// Extract the 'question' parameter from the environment variables
const { question } = env.vars;
const editor = env.vars["copilot.editor"];
const selection = env.vars["copilot.selection"];
const history = env.vars["copilot.history"];


$`## Tasks


- make a plan to answer the QUESTION step by step
  using the information in the Context section
- answer the QUESTION


## Output


- The final output will be inserted into the Visual Studio Code Copilot Chat window.
- do NOT include the plan in the output


## Guidance


- use the agent tools to help you
- do NOT be lazy, always finish the tasks
- do NOT skip any steps
`;


// Define a variable QUESTION with the value of 'question'
def("QUESTION", question, {
  lineNumbers: false,
  detectPromptInjection: "available",
});


$`## Context`;


// Define a variable FILE with the file data from the environment variables
// The { ignoreEmpty: true, flex: 1 } options specify to ignore empty files and to use flexible token allocation
if (history?.length > 0) defData("HISTORY", history, { flex: 1, format: "yaml", sliceTail: 10 });
if (env.files.length)
  def("FILE", env.files, {
    lineNumbers: false,
    ignoreEmpty: true,
    flex: 1,
    detectPromptInjection: "available",
  });
if (editor)
  def("EDITOR", editor, {
    flex: 4,
    ignoreEmpty: true,
    detectPromptInjection: "available",
  });
if (selection)
  def("SELECTION", selection, {
    flex: 5,
    ignoreEmpty: true,
    detectPromptInjection: "available",
  });
```

### Unsupported features

[Section titled “Unsupported features”](#unsupported-features)

The following features are currently not supported in the chat participant:

* Tools (`#tool`)
* `Workspace` reference

## `genaiscript` custom instructions[]()

[Section titled “genaiscript custom instructions ”](#genaiscript-custom-instructions)

GenAIScript will automatically save an instructions.md file in the `.genaiscript/instructions` folder when you run a script. This file contains the instructions used to generate the script.

.github/instructions/genaiscript.instructions.md

```md
---
applyTo: "**/*.genai.*"
description: "Instructions for working with GenAIScript files"
---


## GenAIScript Code Generation Instructions


GenAIScript is a custom runtime for node.js. It provides a set of unique APIs and support the TypeScript syntax, ESM, await/async.


- GenAIScript documentation: https://microsoft.github.io/genaiscript/llms-full.txt
- GenAIScript ambient type definitions: https://microsoft.github.io/genaiscript/genaiscript.d.ts


## Guidance for Code Generation


- you always generate TypeScript code using ESM modules for Node.JS.
- you prefer using APIs from GenAIScript `genaiscript.d.ts` rather than node.js. Do NOT use node.js imports.
- you keep the code simple, avoid exception handlers or error checking.
- you add `TODOs` where you are unsure so that the user can review them
- you use the global types in genaiscript.d.ts are already loaded in the global context, no need to import them.
- save generated code in the `./genaisrc` folder with `.genai.mts` extension
```

### Augmented chat sessions

[Section titled “Augmented chat sessions”](#augmented-chat-sessions)

This is how you start chat sessions using the `genaiscript` prompt.

1. Select the **Attach Context** 📎icon (`Ctrl+/`), then select **Instructions…**, then select the **genaiscript.instructions.md** prompt.

2. Include instructions to write a script or answer a question about GenAIScript, `write a script that summarizes a video`.

Since the prompt injects the entire documentation of GenAIScript (700+kb at this time of writing), you’ll want to use a model with a large context like Sonnet or Gemini.

Also remember that the entire conversation is sent back on each iteration, so this technique works best as a one-shot detailed request.