Skip to content

GitHub Copilot Chat

GenAIScript integrates with 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.

@genaiscript chat participant

The @genaiscript chat participant lets your run scripts without the context of a GitHub Copilot Chat conversation. This is useful for leverage existing scripts in an interactive chat session.

A screenshot of the chat participant window.

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.

Terminal window
@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.

Terminal window
@genaiscript add comments to the current editor

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
script({
model: "github_copilot_chat:current"
})
  • gpt-4o-mini: github_copilot_chat:gpt-4o-mini
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

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

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

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

  • mermaid will generate a diagram from the user prompt.
mermaid.genai.mjs
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
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
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

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

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

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

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
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

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

  • Tools (#tool)
  • Workspace reference

genaiscript custom prompt

GenAIScript will automatically save a prompt file and additional files in .genaiscript/docs to provide a better context for using Copilot Chat to generate GenAIScript scripts.

Play

The genaiscript.prompt.md file is a reusable prompt file that provides additional context to help GitHub Copilot Chat answering GenAIScript code generation queries.

.genaiscript/prompts/genaiscript.prompt.md
## Role
You are an expert at the GenAIScript programming language (https://microsoft.github.io/genaiscript). Your task is to generate GenAIScript script
or answer questions about GenAIScript.
## Reference
- [GenAIScript docs](../../.genaiscript/docs/llms-full.txt)
## 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 node.js. Avoid 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

To use this prompt in your chat,

Enable reusable prompts

At this time, the reusable prompt feature is experimental and needs to be enabled in the settings.

  1. Open the settings (Ctrl+,) and search for GitHub Copilot Chat. Set the chat.promptFiles setting to true.

  2. Add .genaiscript/prompts to the list of folders to search for prompt files.

  3. Open the command palette (Ctrl+Shift+P) and select GitHub Copilot: Build Local Workspace Index to create a local index to help with queries with large files (like the GenAIScript documentation!).

Augmented chat sessions

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

  1. Select the Attach Context 📎icon (Ctrl+/), then select Prompt…, then select the genaiscript 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 detailled request.