Files
vscode-extension-samples/chat-agent-sample/src/extension.ts

116 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-11-13 16:30:27 +01:00
import * as vscode from 'vscode';
const MEOW_COMMAND_ID = 'cat.meow';
2023-11-13 15:59:54 -06:00
interface ICatChatAgentResult extends vscode.ChatAgentResult2 {
slashCommand: string;
}
2023-11-13 16:30:27 +01:00
export function activate(context: vscode.ExtensionContext) {
// Define a Cat chat agent handler.
2023-11-13 15:59:54 -06:00
const handler: vscode.ChatAgentHandler = async (request: vscode.ChatAgentRequest, context: vscode.ChatAgentContext, progress: vscode.Progress<vscode.ChatAgentProgress>, token: vscode.CancellationToken): Promise<ICatChatAgentResult> => {
2023-11-13 16:30:27 +01:00
// To talk to an LLM in your slash command handler implementation, your
// extension can use VS Code's `requestChatAccess` API to access the Copilot API.
2023-11-13 15:59:54 -06:00
// The GitHub Copilot Chat extension implements this provider.
2024-01-17 17:25:47 -03:00
if (request.subCommand == 'teach') {
2023-11-13 16:30:27 +01:00
const access = await vscode.chat.requestChatAccess('copilot');
2023-11-13 15:59:54 -06:00
const topics = ['linked list', 'recursion', 'stack', 'queue', 'pointers'];
2023-11-13 16:30:27 +01:00
const topic = topics[Math.floor(Math.random() * topics.length)];
const messages = [
{
role: vscode.ChatMessageRole.System,
2023-11-13 15:59:54 -06:00
content: 'You are a cat! Your job is to explain computer science concepts in the funny manner of a cat. Always start your response by stating what concept you are explaining.'
2023-11-13 16:30:27 +01:00
},
{
role: vscode.ChatMessageRole.User,
content: topic
},
];
const chatRequest = access.makeRequest(messages, {}, token);
for await (const fragment of chatRequest.response) {
2023-11-13 19:56:09 +01:00
progress.report({ content: fragment });
2023-11-13 16:30:27 +01:00
}
2023-11-13 15:59:54 -06:00
return { slashCommand: 'teach' };
2024-01-17 17:25:47 -03:00
} else if (request.subCommand == 'play') {
2023-11-13 16:30:27 +01:00
const access = await vscode.chat.requestChatAccess('copilot');
const messages = [
{
role: vscode.ChatMessageRole.System,
2023-11-13 15:59:54 -06:00
content: 'You are a cat that wants to play! Reply in a helpful way for a coder, but with the hidden meaning that all you want to do is play.'
2023-11-13 16:30:27 +01:00
},
{
role: vscode.ChatMessageRole.User,
content: request.prompt
}
2023-11-13 16:30:27 +01:00
];
const chatRequest = access.makeRequest(messages, {}, token);
for await (const fragment of chatRequest.response) {
2023-11-13 19:56:09 +01:00
progress.report({ content: fragment });
2023-11-13 16:30:27 +01:00
}
2023-11-13 15:59:54 -06:00
return { slashCommand: 'play' };
} else {
const access = await vscode.chat.requestChatAccess('copilot');
const messages = [
{
role: vscode.ChatMessageRole.System,
content: 'You are a cat! Reply in the voice of a cat, using cat analogies when appropriate.'
},
{
role: vscode.ChatMessageRole.User,
content: request.prompt
}
];
const chatRequest = access.makeRequest(messages, {}, token);
for await (const fragment of chatRequest.response) {
progress.report({ content: fragment });
}
return { slashCommand: '' };
}
2023-11-13 16:30:27 +01:00
};
// Agents appear as top-level options in the chat input
// when you type `@`, and can contribute sub-commands in the chat input
// that appear when you type `/`.
const agent = vscode.chat.createChatAgent('cat', handler);
agent.iconPath = vscode.Uri.joinPath(context.extensionUri, 'cat.jpeg');
agent.description = vscode.l10n.t('Meow! What can I help you with?');
agent.fullName = vscode.l10n.t('Cat');
agent.slashCommandProvider = {
provideSlashCommands(token) {
return [
{ name: 'teach', description: 'Pick at random a computer science concept then explain it in purfect way of a cat' },
{ name: 'play', description: 'Do whatever you want, you are a cat after all' }
];
}
};
agent.followupProvider = {
2023-11-13 15:59:54 -06:00
provideFollowups(result: ICatChatAgentResult, token: vscode.CancellationToken) {
if (result.slashCommand === 'teach') {
2023-11-13 16:30:27 +01:00
return [{
commandId: MEOW_COMMAND_ID,
message: '@cat thank you',
title: vscode.l10n.t('Meow!')
}];
2023-11-13 15:59:54 -06:00
} else if (result.slashCommand === 'play') {
2023-11-13 16:30:27 +01:00
return [{
message: '@cat let us play',
title: vscode.l10n.t('Play with the cat')
}];
}
}
};
context.subscriptions.push(
agent,
// Register the command handler for the /meow followup
vscode.commands.registerCommand(MEOW_COMMAND_ID, async () => {
vscode.window.showInformationMessage('Meow!');
}),
);
}
export function deactivate() { }