Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vscode][3/n] Setup Env Variables: Use env template file for new files #1283

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand(
COMMANDS.SETUP_ENVIRONMENT_VARIABLES,
() => {
vscode.window.showInformationMessage("Will implement next PR");
setupEnvironmentVariables(context);
}
)
);
Expand Down Expand Up @@ -716,6 +716,86 @@ async function checkPip() {
});
}

/**
* Creates an .env file (or opens it if it already exists) to define environment variables
* 1) If .env file exists:
* a) Add helper lines on how to add common API keys (if not currently present)
* 2) If .env file doesn't exist
* b) Add template file containing helper lines from 1a above
*/
async function setupEnvironmentVariables(context: vscode.ExtensionContext) {
// Use home dir because env variables should be global. I get the argument
// for having in the workspace dir. I personally feel this is more
// annoying to setup every time you create a new project when using the
// same API keys, but I can do whatever option you want, not hard to
// implement
const homedir = require("os").homedir(); // This is cross-platform: https://stackoverflow.com/a/9081436
const defaultEnvPath = path.join(homedir, ".env");

const envPath = await vscode.window.showInputBox({
prompt: "Enter the path of your .env file",
value: defaultEnvPath,
validateInput: (text) => {
if (!text) {
return "File path is required";
} else if (!text.endsWith(".env")) {
return "File path must end in .env file";
}
// TODO: Check that file path is a "/.env" file (linux) or "\.env" (Windows)

// TODO: Check that env path is contained within workspace hierarchy
// (Ex: can't have .env file in a sibling dir otherwise AIConfig
// loadenv can't read it)
return null;
},
});

if (!envPath) {
vscode.window.showInformationMessage(
"Environment variable setup cancelled"
);
return;
}

if (fs.existsSync(envPath)) {
vscode.window.showInformationMessage(
"Env file already exists, will implement next PR"
);
} else {
// Create the .env file from the sample
const envTemplatePath = vscode.Uri.joinPath(
context.extensionUri,
"static",
"env_template.env"
);

try {
await vscode.workspace.fs.copy(
envTemplatePath,
vscode.Uri.file(envPath),
{ overwrite: false }
);
} catch (err) {
vscode.window.showErrorMessage(
`Error creating new file ${envTemplatePath}: ${err}`
);
}

const doc = await vscode.workspace.openTextDocument(envPath);
if (doc) {
vscode.window.showTextDocument(doc, {
preview: false,
// Tried using vscode.ViewColumn.Active but that overrides existing
// walkthrough window
viewColumn: vscode.ViewColumn.Beside,
});
vscode.window.showInformationMessage(
"Please define your environment variables."
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what if it doesn't open?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will follow up someday

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

async function shareAIConfig(
context: vscode.ExtensionContext,
aiconfigEditorManager: AIConfigEditorManager
Expand Down
15 changes: 15 additions & 0 deletions vscode-extension/static/env_template.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Uncomment the following lines to set these common env variables for
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tanya said this is too much text. I'm just going to land and we can fix later

# accessing generative AI models. If your model requires other env variables
# you can also set those in this file

## Used for models from OpenAI such as Chat GPT and Dall-E
## You can get your api key from https://platform.openai.com/api-keys
# OPENAI_API_KEY=<your key here>

## Used for models from Google such as Gemini and Gemma
## You can get your api key from https://ai.google.dev/tutorials/setup
# GOOGLE_API_KEY=<your key here>

## Used for models hosted on Hugging Face: https://huggingface.co/models
## You can get your token from https://huggingface.co/settings/tokens
# HUGGING_FACE_API_TOKEN=<your_token_here>