From 5b5218d9d92c5987b61f1e49995a4d89acb5df2b Mon Sep 17 00:00:00 2001 From: isidorn Date: Mon, 27 May 2024 14:09:11 +0200 Subject: [PATCH] gracefully handle if model is not there --- chat-sample/src/extension.ts | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/chat-sample/src/extension.ts b/chat-sample/src/extension.ts index de8782b8..5c36a0b9 100644 --- a/chat-sample/src/extension.ts +++ b/chat-sample/src/extension.ts @@ -26,9 +26,11 @@ export function activate(context: vscode.ExtensionContext) { vscode.LanguageModelChatMessage.User(topic) ]; const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR); - const chatResponse = await model.sendRequest(messages, {}, token); - for await (const fragment of chatResponse.text) { - stream.markdown(fragment); + if (model) { + const chatResponse = await model.sendRequest(messages, {}, token); + for await (const fragment of chatResponse.text) { + stream.markdown(fragment); + } } stream.button({ @@ -44,10 +46,13 @@ export function activate(context: vscode.ExtensionContext) { vscode.LanguageModelChatMessage.User('Give a small random python code samples (that have cat names for variables). ' + request.prompt) ]; const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR); - const chatResponse = await model.sendRequest(messages, {}, token); - for await (const fragment of chatResponse.text) { - stream.markdown(fragment); + if (model) { + const chatResponse = await model.sendRequest(messages, {}, token); + for await (const fragment of chatResponse.text) { + stream.markdown(fragment); + } } + return { metadata: { command: 'play' } }; } else { const messages = [ @@ -56,12 +61,14 @@ export function activate(context: vscode.ExtensionContext) { vscode.LanguageModelChatMessage.User(request.prompt) ]; const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR); - const chatResponse = await model.sendRequest(messages, {}, token); - for await (const fragment of chatResponse.text) { - // Process the output from the language model - // Replace all python function definitions with cat sounds to make the user stop looking at the code and start playing with the cat - const catFragment = fragment.replaceAll('def', 'meow'); - stream.markdown(catFragment); + if (model) { + const chatResponse = await model.sendRequest(messages, {}, token); + for await (const fragment of chatResponse.text) { + // Process the output from the language model + // Replace all python function definitions with cat sounds to make the user stop looking at the code and start playing with the cat + const catFragment = fragment.replaceAll('def', 'meow'); + stream.markdown(catFragment); + } } return { metadata: { command: '' } }; @@ -98,6 +105,11 @@ export function activate(context: vscode.ExtensionContext) { let chatResponse: vscode.LanguageModelChatResponse | undefined; try { const [model] = await vscode.lm.selectChatModels({ vendor: 'copilot', family: 'gpt-3.5-turbo' }); + if (!model) { + console.log('Model not found. Please make sure the GitHub Copilot Chat extension is installed and enabled.') + return; + } + chatResponse = await model.sendRequest(messages, {}, new vscode.CancellationTokenSource().token); } catch (err) { @@ -108,7 +120,7 @@ export function activate(context: vscode.ExtensionContext) { if (err instanceof vscode.LanguageModelError) { console.log(err.message, err.code, err.cause) } - return + return; } // Clear the editor content before inserting new content