Last active
January 14, 2025 12:22
-
-
Save laiso/ef9397c9aa63f579119823ba251e2ac6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as vscode from 'vscode'; | |
/** | |
* 拡張機能が最初に有効化されると呼び出されるメソッド | |
*/ | |
export function activate(context: vscode.ExtensionContext) { | |
// 「extension.generateCodeAnnotation」コマンドを登録 | |
const generateCommand = vscode.commands.registerCommand('myAIextension.generateCodeAnnotation', async () => { | |
await generateCodeAnnotation(); | |
}); | |
context.subscriptions.push(generateCommand); | |
} | |
/** | |
* 拡張機能が無効化されたときに呼ばれるメソッド | |
*/ | |
export function deactivate() { | |
// 特に処理は不要 | |
} | |
/** | |
* AIにコード注釈を生成させる関数の例 | |
*/ | |
async function generateCodeAnnotation() { | |
// 例: 「猫がコンピュータサイエンスを面白おかしく解説する」 という指示 | |
const prompt = [ | |
vscode.LanguageModelChatMessage.User( | |
'あなたは猫です!猫の比喩を使ってコンピュータサイエンスの概念を面白おかしく説明してください。必ず概念名を述べ、コードサンプルを含めてください。' | |
), | |
vscode.LanguageModelChatMessage.User( | |
'再帰を理解したいです' | |
) | |
]; | |
await sendPromptToModel(prompt); | |
} | |
/** | |
* Copilotモデルにプロンプトを送信し、結果を受け取る | |
*/ | |
async function sendPromptToModel(prompt: vscode.LanguageModelChatMessage[]) { | |
const [model] = await vscode.lm.selectChatModels({ | |
vendor: 'copilot', | |
family: 'gpt-4o', | |
}); | |
if (!model) { | |
vscode.window.showErrorMessage('Copilotモデルが見つかりませんでした。'); | |
return; | |
} | |
try { | |
const response = await model.sendRequest(prompt, {}); | |
for await (const fragment of response.text) { | |
const editor = vscode.window.activeTextEditor; | |
if (editor) { | |
const document = editor.document; | |
const position = editor.selection.active; | |
await editor.edit(editBuilder => { | |
editBuilder.insert(position, fragment); | |
}); | |
} else { | |
vscode.window.showErrorMessage('アクティブなエディタが見つかりませんでした。'); | |
} | |
} | |
} catch (err) { | |
vscode.window.showErrorMessage('モデルのリクエストに失敗しました: ' + String(err)); | |
} | |
} |
Author
laiso
commented
Jan 14, 2025

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment