Skip to content

Commit

Permalink
Merge pull request #971 from microsoft/isidorn/active-bison
Browse files Browse the repository at this point in the history
Add command to use cat names in editor
  • Loading branch information
isidorn authored Feb 23, 2024
2 parents d9a3291 + 21d6b65 commit a7e7e7f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 8 additions & 0 deletions chat-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
"chatVariableResolver",
"languageModels"
],
"contributes": {
"commands": [
{
"command": "cat.namesInEditor",
"title": "Use Cat Names in Editor"
}
]
},
"main": "./out/extension.js",
"scripts": {
"vscode:prepublish": "npm run compile",
Expand Down
34 changes: 29 additions & 5 deletions chat-sample/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';

const MEOW_COMMAND_ID = 'cat.meow';
const CAT_NAMES_COMMAND_ID = 'cat.namesInEditor';
const CAT_PARTICIPANT_NAME = 'cat';

interface ICatChatResult extends vscode.ChatResult {
Expand Down Expand Up @@ -32,8 +32,8 @@ export function activate(context: vscode.ExtensionContext) {
}

stream.button({
command: MEOW_COMMAND_ID,
title: vscode.l10n.t('Meow!')
command: CAT_NAMES_COMMAND_ID,
title: vscode.l10n.t('Use Cat Names in Editor')
});

return { metadata: { command: 'teach' } };
Expand Down Expand Up @@ -119,8 +119,32 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
cat,
// Register the command handler for the /meow followup
vscode.commands.registerCommand(MEOW_COMMAND_ID, async () => {
vscode.window.showInformationMessage('Meow!');
vscode.commands.registerTextEditorCommand(CAT_NAMES_COMMAND_ID, async (textEditor: vscode.TextEditor) => {
// Replace all variables in active editor with cat names and words
const text = textEditor.document.getText();
const access = await vscode.lm.requestLanguageModelAccess(LANGUAGE_MODEL_ID);
const messages = [
new vscode.LanguageModelSystemMessage(`You are a cat! Think carefully and step by step like a cat would.
Your job is to replace all variable names in the following code with funny cat variable names. Be creative. IMPORTANT respond just with code. Do not use markdown!`),
new vscode.LanguageModelUserMessage(text)
];
const chatRequest = access.makeChatRequest(messages, {}, new vscode.CancellationTokenSource().token);

// Clear the editor content before inserting new content
await textEditor.edit(edit => {
const start = new vscode.Position(0, 0);
const end = new vscode.Position(textEditor.document.lineCount - 1, textEditor.document.lineAt(textEditor.document.lineCount - 1).text.length);
edit.delete(new vscode.Range(start, end));
});

// Stream the code into the editor as it is coming in from the Language Model
for await (const fragment of chatRequest.stream) {
await textEditor.edit(edit => {
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
const position = new vscode.Position(lastLine.lineNumber, lastLine.text.length);
edit.insert(position, fragment);
});
}
}),
);
}
Expand Down

0 comments on commit a7e7e7f

Please sign in to comment.