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

Function handler

chat-sample/src/simple.ts:17–89  ·  view source on GitHub ↗
(request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken)

Source from the content-addressed store, hash-verified

15
16 // Define a Cat chat handler.
17 const handler: vscode.ChatRequestHandler = async (request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken): Promise<ICatChatResult> => {
18 // To talk to an LLM in your subcommand handler implementation, your
19 // extension can use VS Code's `requestChatAccess` API to access the Copilot API.
20 // The GitHub Copilot Chat extension implements this provider.
21 if (request.command === 'randomTeach') {
22 stream.progress('Picking the right topic to teach...');
23 const topic = getTopic(context.history);
24 try {
25 const messages = [
26 vscode.LanguageModelChatMessage.User('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. Always include code samples.'),
27 vscode.LanguageModelChatMessage.User(topic)
28 ];
29
30 const chatResponse = await request.model.sendRequest(messages, {}, token);
31 for await (const fragment of chatResponse.text) {
32 stream.markdown(fragment);
33 }
34
35 } catch (err) {
36 handleError(logger, err, stream);
37 }
38
39 stream.button({
40 command: CAT_NAMES_COMMAND_ID,
41 title: vscode.l10n.t('Use Cat Names in Editor')
42 });
43
44 logger.logUsage('request', { kind: 'randomTeach' });
45 return { metadata: { command: 'randomTeach' } };
46 } else if (request.command === 'play') {
47 stream.progress('Throwing away the computer science books and preparing to play with some Python code...');
48 try {
49 // Here's an example of how to use the prompt-tsx library to build a prompt
50 const { messages } = await renderPrompt(
51 PlayPrompt,
52 { userQuery: request.prompt },
53 { modelMaxPromptTokens: request.model.maxInputTokens },
54 request.model);
55
56 const chatResponse = await request.model.sendRequest(messages, {}, token);
57 for await (const fragment of chatResponse.text) {
58 stream.markdown(fragment);
59 }
60
61 } catch (err) {
62 handleError(logger, err, stream);
63 }
64
65 logger.logUsage('request', { kind: 'play' });
66 return { metadata: { command: 'play' } };
67 } else {
68 try {
69 const messages = [
70 vscode.LanguageModelChatMessage.User(`You are a cat! Think carefully and step by step like a cat would.
71 Your job is to explain computer science concepts in the funny manner of a cat, using cat metaphors. Always start your response by stating what concept you are explaining. Always include code samples.`),
72 vscode.LanguageModelChatMessage.User(request.prompt)
73 ];
74

Callers

nothing calls this directly

Calls 2

getTopicFunction · 0.85
handleErrorFunction · 0.85

Tested by

no test coverage detected