Skip to content

GitHub Copilot Chat

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

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"
}
]

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({
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_vision",
"system.agent_web",
],
group: "copilot", // 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 })
def("FILE", env.files, {
lineNumbers: false,
ignoreEmpty: true,
flex: 1,
detectPromptInjection: "available",
})
def("EDITOR", editor, {
flex: 4,
ignoreEmpty: true,
detectPromptInjection: "available",
})
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