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

Feature: Register OpenAI API key #31

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@
"title": "Restart LSP Server",
"icon": "$(debug-restart)",
"category": "ZenML Environment"
},
{
"command": "zenml.registerOpenAIAPIKey",
"title": "Register OpenAI API Key",
sklarfox marked this conversation as resolved.
Show resolved Hide resolved
"icon": "$(add)",
"category": "ZenML Secrets"
}
],
"viewsContainers": {
Expand Down
44 changes: 44 additions & 0 deletions src/commands/secrets/cmds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright(c) ZenML GmbH 2024. All Rights Reserved.
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied.See the License for the specific language governing
// permissions and limitations under the License.

import * as vscode from 'vscode';
import type { ExtensionContext } from 'vscode';

// TODO I don't think retrieval of an api key will live in here
sklarfox marked this conversation as resolved.
Show resolved Hide resolved

const registerOpenAIAPIKey = async (context: ExtensionContext) => {
let apiKey = await context.secrets.get('OPENAI_API_KEY');

if (apiKey) {
apiKey = await vscode.window.showInputBox({
prompt: 'OpenAI API Key already exists, enter a new value to update.',
password: true,
});
} else {
apiKey = await vscode.window.showInputBox({
prompt: 'Please enter your OpenAI API key',
password: true,
});
}

if (apiKey === undefined) {
return undefined;
}

await context.secrets.store('OPENAI_API_KEY', apiKey);
vscode.window.showInformationMessage('OpenAI API key stored successfully.');
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error handling for user input.

Currently, if showInputBox returns undefined, the function returns without storing the API key. Consider handling this case explicitly by showing a message to the user.

Apply this diff to improve user feedback:

  if (apiKey === undefined) {
-   return undefined;
+   vscode.window.showWarningMessage('API key input was canceled.');
+   return;
  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const registerOpenAIAPIKey = async (context: ExtensionContext) => {
let apiKey = await context.secrets.get('OPENAI_API_KEY');
if (apiKey) {
apiKey = await vscode.window.showInputBox({
prompt: 'OpenAI API Key already exists, enter a new value to update.',
password: true,
});
} else {
apiKey = await vscode.window.showInputBox({
prompt: 'Please enter your OpenAI API key',
password: true,
});
}
if (apiKey === undefined) {
return undefined;
}
await context.secrets.store('OPENAI_API_KEY', apiKey);
vscode.window.showInformationMessage('OpenAI API key stored successfully.');
};
const registerOpenAIAPIKey = async (context: ExtensionContext) => {
let apiKey = await context.secrets.get('OPENAI_API_KEY');
if (apiKey) {
apiKey = await vscode.window.showInputBox({
prompt: 'OpenAI API Key already exists, enter a new value to update.',
password: true,
});
} else {
apiKey = await vscode.window.showInputBox({
prompt: 'Please enter your OpenAI API key',
password: true,
});
}
if (apiKey === undefined) {
vscode.window.showWarningMessage('API key input was canceled.');
return;
}
await context.secrets.store('OPENAI_API_KEY', apiKey);
vscode.window.showInformationMessage('OpenAI API key stored successfully.');
};


export const secretsCommands = {
registerOpenAIAPIKey,
};
sklarfox marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions src/commands/secrets/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright(c) ZenML GmbH 2024. All Rights Reserved.
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied.See the License for the specific language governing
// permissions and limitations under the License.

// TODO the registration of secrets commands should go in here, which is then imported into the ZenExtension file
// In registry, the "register command thing is passed context (i.e. context.secrets) as an arg"

import { secretsCommands } from './cmds';
import { registerCommand } from '../../common/vscodeapi';
import { ZenExtension } from '../../services/ZenExtension';
import { ExtensionContext, commands } from 'vscode';

/**
* Registers pipeline-related commands for the extension.
*
* @param {ExtensionContext} context - The context in which the extension operates, used for registering commands and managing their lifecycle.
*/
export const registerSecretsCommands = (context: ExtensionContext) => {
try {
const registeredCommands = [
registerCommand(
'zenml.registerOpenAIAPIKey',
async () => await secretsCommands.registerOpenAIAPIKey(context)
),
];

registeredCommands.forEach(cmd => {
context.subscriptions.push(cmd);
ZenExtension.commandDisposables.push(cmd);
});

commands.executeCommand('setContext', 'secretsCommandsRegistered', true);
} catch (error) {
console.error('Error registering pipeline commands:', error);
commands.executeCommand('setContext', 'secretsCommandsRegistered', false);
}
};
12 changes: 12 additions & 0 deletions src/common/vscodeapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ConfigurationScope,
Disposable,
DocumentSelector,
ExtensionContext,
languages,
LanguageStatusItem,
LogOutputChannel,
Expand Down Expand Up @@ -69,3 +70,14 @@ export function createLanguageStatusItem(
): LanguageStatusItem {
return languages.createLanguageStatusItem(id, selector);
}

export async function getSecret(context: ExtensionContext, key: string) {
const secret = await context.secrets.get(key);

if (secret === undefined) {
console.error(`The requested secret with key '${key}' does not exist.`);
return;
}

return secret;
}
2 changes: 2 additions & 0 deletions src/services/ZenExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as vscode from 'vscode';
import { registerPipelineCommands } from '../commands/pipelines/registry';
import { registerServerCommands } from '../commands/server/registry';
import { registerStackCommands } from '../commands/stack/registry';
import { registerSecretsCommands } from '../commands/secrets/registry';
import { EXTENSION_ROOT_DIR } from '../common/constants';
import { registerLogger, traceLog, traceVerbose } from '../common/log/logging';
import {
Expand Down Expand Up @@ -73,6 +74,7 @@ export class ZenExtension {
registerStackCommands,
registerComponentCommands,
registerPipelineCommands,
registerSecretsCommands,
];

/**
Expand Down
Loading