MCPcopy Create free account
hub / github.com/microsoft/vscode-extension-samples / activate

Function activate

chat-tutorial/src/extension.ts:7–59  ·  view source on GitHub ↗
(context: vscode.ExtensionContext)

Source from the content-addressed store, hash-verified

5const EXERCISES_PROMPT = 'You are a helpful tutor. Your job is to teach the user with fun, simple exercises that they can complete in the editor. Your exercises should start simple and get more complex as the user progresses. Move one concept at a time, and do not move on to the next concept until the user provides the correct answer. Give hints in your exercises to help the user learn. If the user is stuck, you can provide the answer and explain why it is the answer. If the user asks a non-programming question, politely decline to respond.';
6
7export function activate(context: vscode.ExtensionContext) {
8
9 // define a chat handler
10 const handler: vscode.ChatRequestHandler = async (request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken) => {
11
12 // initialize the prompt
13 let prompt = BASE_PROMPT;
14
15 if (request.command === 'exercise') {
16 prompt = EXERCISES_PROMPT;
17 }
18
19 // initialize the messages array with the prompt
20 const messages = [
21 vscode.LanguageModelChatMessage.User(prompt),
22 ];
23
24 // get all the previous participant messages
25 const previousMessages = context.history.filter(
26 (h) => h instanceof vscode.ChatResponseTurn
27 );
28
29 // add the previous messages to the messages array
30 previousMessages.forEach((m) => {
31 let fullMessage = '';
32 m.response.forEach((r) => {
33 const mdPart = r as vscode.ChatResponseMarkdownPart;
34 fullMessage += mdPart.value.value;
35 });
36 messages.push(vscode.LanguageModelChatMessage.Assistant(fullMessage));
37 });
38
39 // add in the user's message
40 messages.push(vscode.LanguageModelChatMessage.User(request.prompt));
41
42 // send the request
43 const chatResponse = await request.model.sendRequest(messages, {}, token);
44
45 // stream the response
46 for await (const fragment of chatResponse.text) {
47 stream.markdown(fragment);
48 }
49
50 return;
51
52 };
53
54 // create participant
55 const tutor = vscode.chat.createChatParticipant("chat-tutorial.code-tutor", handler);
56
57 // add icon to participant
58 tutor.iconPath = vscode.Uri.joinPath(context.extensionUri, 'tutor.jpeg');
59}
60
61export function deactivate() { }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected