I want to write a simple VS Code extension that calls OpenAI API and tells the user the day in Danish.
I cannot find the extension when searching for it in the Extension Development Host window (after pressing F5). What could I be doing wrong ?
Thanks in advance.
****** Here is the code in extension.js ******:
const vscode = require('vscode');
import { OpenAI } from 'openai';
require("dotenv").config(); // Load environment variables from .env file
// Initialize the OpenAI client
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Activate the extension
function activate(context) {
console.log('Your extension "gpt-danish-day" is now active!');
// Register the command
let disposable = vscode.commands.registerCommand('gpt-danish-day.getDay', async () => {
// Call the OpenAI function to get the day in Danish
const response = await getDayInDanish();
// Show the result in a VS Code information message
vscode.window.showInformationMessage(`Today in Danish: ${response}`);
});
context.subscriptions.push(disposable);
}
// Function to fetch the current day in Danish
async function getDayInDanish() {
try {
const chatCompletion = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Tell me what day is it today in Danish?" }
]
});
// Extract and return the content of the first choice
return chatCompletion.choices[0].message.content.trim();
} catch (error) {
console.error("Error calling OpenAI API:", error.message);
return "Error fetching the day in Danish.";
}
}
// Deactivate the extension
function deactivate() {}
module.exports = {
activate,
deactivate
};
****** Here is the code in package.json ******:
{
“name”: “code-commenter”,
“displayName”: “Code Commenter”,
“description”: “A VS Code extension that uses ChatGPT to add comments to your code.”,
“version”: “0.0.1”,
“engines”: {
“vscode”: “^1.80.0”
},
“categories”: [
“Other”
],
“contributes”: {
“commands”: [
{
“command”: “gpt-danish-day.getDay”,
“title”: “Get Day in Danish”
}
]
}, “scripts”: {
“vscode:prepublish”: “vsce package”,
“test”: “node ./test/runTest.js”
},
“dependencies”: {
“axios”: “^1.5.0”,
“dotenv”: “^10.0.0”,
“vscode”: “^1.80.0”
},
“devDependencies”: {}
}


