Skip to content

Commit

Permalink
[vscode] 7/n listeners and .env
Browse files Browse the repository at this point in the history
Summary:

When the .env file path is set, vscode will invoke api/set_env on each AIConfig server.

Defines a listener to a newly introduced setting for the env_file_path.

This ensures that users do not need to restart vscode to pickup env changes.

Test Plan:

1. set env file path
2. changes picked up
3. changes picked up on restart
4. Changes picked up when manually removing vscode setting and re-setting env file path and api key

https://github.com/lastmile-ai/aiconfig/assets/141073967/07ac1d43-28ef-439f-98e7-94a4e73e11c9
  • Loading branch information
Ankush Pala [email protected] committed Mar 4, 2024
1 parent d48a38e commit 8df06ee
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
"type": "string",
"default": "",
"description": "Version of the AIConfig Editor extension last time it was activated. We use this value to check if the extension has been updated to prompt users to reload VS Code"
},
"vscode-aiconfig.env_file_path": {
"type": "string",
"default": "",
"description": "env_file_path"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions vscode-extension/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const PYTHON_INTERPRETER_CACHE_KEY_NAME = "pythonInterpreter";
// Used for prompting user to reload VS Code window on update, and ensuring
// that walkthrough gets shown on first install
export const VERSION_KEY_NAME = "version";

export const ENV_FILE_PATH = "env_file_path";
4 changes: 4 additions & 0 deletions vscode-extension/src/editor_server/editorServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EXTENSION_NAME } from "../util";
import { getPythonPath } from "../utilities/pythonSetupUtils";
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import serverPortManager from "./serverPortManager";
import { ENV_FILE_PATH } from "../constants";

export enum EditorServerState {
Starting = "Starting",
Expand Down Expand Up @@ -73,6 +74,8 @@ export class EditorServer {
const modelRegistryPathArgs = modelRegistryPath
? ["--parsers-module-path", modelRegistryPath]
: [];
const filePath = config.get(ENV_FILE_PATH) as string;
const envFilePathArgs = filePath ? ["--env-file-path", filePath] : [];

this.port = await serverPortManager.getPort();

Expand All @@ -89,6 +92,7 @@ export class EditorServer {
"--server-port",
this.port.toString(),
...modelRegistryPathArgs,
...envFilePathArgs,
],
{
cwd: this.cwd,
Expand Down
21 changes: 21 additions & 0 deletions vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import {
setupEnvironmentVariables,
getConfigurationTarget,
getModeFromDocument,
updateServerEnv
} from "./util";
import {
initialize,
installDependencies,
savePythonInterpreterToCache,
} from "./utilities/pythonSetupUtils";
import { performVersionInstallAndUpdateActionsIfNeeded } from "./utilities/versionUpdateUtils";
import { ENV_FILE_PATH } from "./constants";

// This method is called when your extension is activated
// Your extension is activated the very first time a command is executed
Expand Down Expand Up @@ -208,6 +210,25 @@ export async function activate(context: vscode.ExtensionContext) {
}
)
);

// Handle changes to the .env path
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async (event) => {
if (event.affectsConfiguration("vscode-aiconfig")) {
// Get new env
const envPath = vscode.workspace.getConfiguration("vscode-aiconfig").get(ENV_FILE_PATH) as string;
console.log(`New .env path set: ${envPath}`);
// set env on all AIConfig Servers
const editors: Array<AIConfigEditorState> = Array.from(
aiconfigEditorManager.getRegisteredEditors()
);
if (editors.length > 0) {
editors.forEach(async (editor) => {
updateServerEnv(editor.editorServer.url, envPath);
});

}
}
}));
}

// This method is called when your extension is deactivated
Expand Down
17 changes: 17 additions & 0 deletions vscode-extension/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import fs from "fs";
import path from "path";
import os from "os";

import { ENV_FILE_PATH } from "./constants";

export const EXTENSION_NAME = "vscode-aiconfig";
export const COMMANDS = {
INIT: `${EXTENSION_NAME}.init`,
Expand Down Expand Up @@ -100,6 +102,15 @@ export async function updateServerState(
});
}

export async function updateServerEnv(serverUrl: string, filePath: string) {
return await ufetch.post(
EDITOR_SERVER_ROUTE_TABLE.SET_ENV_FILE_PATH(serverUrl),
{
[ENV_FILE_PATH]: filePath,
}
);
}

// Figure out what kind of AIConfig this is that we are loading
export function getModeFromDocument(
document: vscode.TextDocument
Expand Down Expand Up @@ -383,6 +394,12 @@ export async function setupEnvironmentVariables(
"Please define your environment variables."
);
}

// Update Server Env FLow
// Set the .env file path in the settings
// vscode Extension has a listener for changes defined at activation.
const config = vscode.workspace.getConfiguration(EXTENSION_NAME);
await config.update(ENV_FILE_PATH, envPath, getConfigurationTarget());
}

/**
Expand Down

0 comments on commit 8df06ee

Please sign in to comment.