-
Notifications
You must be signed in to change notification settings - Fork 9
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
Feature: Register OpenAI API key #31
Conversation
WalkthroughThe changes introduce functionality for managing OpenAI and Large Language Model (LLM) API keys within a Visual Studio Code extension. This includes the addition of commands for registering these API keys, securely handling secrets, and enhancing the overall capabilities of the ZenML environment. The updates span multiple files, establishing a structured approach to API key management and secret storage. Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
Early access features: disabledWe are currently testing the following features in early access:
Note:
|
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Outside diff range, codebase verification and nitpick comments (4)
src/commands/secrets/cmds.ts (1)
17-17
: Consider removing or addressing TODO comments.The TODO comment suggests that the retrieval of an API key might not belong in this file. Ensure that the comment is addressed or removed if no longer relevant.
src/commands/secrets/registry.ts (2)
14-15
: Consider removing or addressing TODO comments.The TODO comments suggest that the registration of secrets commands should be moved or adjusted. Ensure that these comments are addressed or removed if no longer relevant.
27-45
: Clarify error message for command registration.The error message in the catch block refers to "pipeline commands," which may not accurately describe the commands being registered. Consider updating the message for clarity.
Apply this diff to clarify the error message:
} catch (error) { - console.error('Error registering pipeline commands:', error); + console.error('Error registering secrets commands:', error);src/common/vscodeapi.ts (1)
74-83
: Ensure consistent error handling.The
getSecret
function logs an error if the secret is not found but does not return a specific value. Consider returningundefined
explicitly for consistency.Apply this diff to make the return value explicit:
if (secret === undefined) { console.error(`The requested secret with key '${key}' does not exist.`); - return; + return undefined; }
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (5)
- package.json (1 hunks)
- src/commands/secrets/cmds.ts (1 hunks)
- src/commands/secrets/registry.ts (1 hunks)
- src/common/vscodeapi.ts (2 hunks)
- src/services/ZenExtension.ts (2 hunks)
Additional context used
Path-based instructions (4)
src/commands/secrets/cmds.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.src/commands/secrets/registry.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.src/common/vscodeapi.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.src/services/ZenExtension.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.
Additional comments not posted (2)
src/services/ZenExtension.ts (1)
20-20
: Verify the definition and usage ofregisterSecretsCommands
.Ensure that the
registerSecretsCommands
function is correctly defined and utilized within the extension's context.Run the following script to verify the function's definition and usage:
Also applies to: 77-77
Verification successful
Verification Successful:
registerSecretsCommands
is correctly defined and used.The
registerSecretsCommands
function is defined as an arrow function and exported fromsrc/commands/secrets/registry.ts
. It is correctly integrated within the extension's context. No issues found with its definition or usage.
- File:
src/commands/secrets/registry.ts
- Lines: 27-46
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the definition and usage of `registerSecretsCommands`. # Test: Search for the function definition. Expect: Correct definition and usage. ast-grep --lang typescript --pattern 'function registerSecretsCommands($_) { $$$ }'Length of output: 85
Script:
#!/bin/bash # Description: Search for alternative patterns for `registerSecretsCommands`. # Search for `registerSecretsCommands` as an arrow function. ast-grep --lang typescript --pattern 'const registerSecretsCommands = ($_) => { $$$ }' # Search for `registerSecretsCommands` as a method within a class. ast-grep --lang typescript --pattern 'class $_ { $$$ registerSecretsCommands($_) { $$$ } $$$ }' # Search for `registerSecretsCommands` as a named export. ast-grep --lang typescript --pattern 'export function registerSecretsCommands($_) { $$$ }'Length of output: 1649
package.json (1)
302-305
: Verify the registration and usage ofzenml.registerOpenAIAPIKey
.Ensure that the command is correctly registered and utilized within the extension.
Run the following script to verify the command's registration and usage:
Verification successful
The command
zenml.registerOpenAIAPIKey
is correctly registered and utilized.The command is registered in
src/commands/secrets/registry.ts
and is associated with an asynchronous function call, confirming its proper implementation and usage. No further action is required.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the registration and usage of `zenml.registerOpenAIAPIKey`. # Test: Search for the command registration. Expect: Correct registration and usage. rg --type json 'zenml.registerOpenAIAPIKey'Length of output: 105
Script:
#!/bin/bash # Description: Search for the implementation and usage of `zenml.registerOpenAIAPIKey` in JavaScript or TypeScript files. # Test: Search for the command implementation and usage. Expect: Correct implementation and usage. rg 'zenml.registerOpenAIAPIKey' --type js --type ts -A 5Length of output: 421
src/commands/secrets/cmds.ts
Outdated
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.'); | ||
}; |
There was a problem hiding this comment.
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.
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.'); | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments on slack. This seems good for OpenAI, but we should offer at least the main three providers.
@strickvl Thanks for the review! I've generalized the function to now also support Anthropic and Gemini on top of OpenAI. |
I've made one additional change to the code; I have changed the naming convention for the secrets to prevent collision issues with other extensions. The previous names were |
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range, codebase verification and nitpick comments (2)
src/commands/secrets/registry.ts (1)
24-43
: LGTM with suggestions!The
registerSecretsCommands
function is correctly implemented and follows the best practices for registering commands in a VSCode extension. However, consider the following suggestions to improve the function:
- Add more error handling and logging to help with debugging and troubleshooting.
- Consider using a more descriptive name for the
cmd
variable in theforEach
loop.- Consider adding a comment to explain the purpose of the
setContext
command.src/common/vscodeapi.ts (1)
74-83
: LGTM with a suggestion!The
getSecret
function is well-structured and follows TypeScript best practices. It handles the case when the secret is not found by logging an error and returningundefined
.To improve the code quality and maintainability, consider adding JSDoc comments to document the parameters and return value of the function. Here's an example:
/** * Retrieves a secret from the extension context. * @param context The extension context. * @param key The key of the secret to retrieve. * @returns The secret value if found, otherwise `undefined`. */ export async function getSecret(context: ExtensionContext, key: string) { // ... }
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (4)
- package.json (1 hunks)
- src/commands/secrets/cmds.ts (1 hunks)
- src/commands/secrets/registry.ts (1 hunks)
- src/common/vscodeapi.ts (2 hunks)
Additional context used
Path-based instructions (3)
src/commands/secrets/registry.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.src/commands/secrets/cmds.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.src/common/vscodeapi.ts (1)
Pattern
**/*.ts
: Review the Typescript code for conformity with industry standards and best practices, highlighting any deviations.
Additional comments not posted (5)
src/commands/secrets/registry.ts (2)
1-12
: LGTM!The copyright and license header is correctly formatted and contains the necessary information.
14-17
: LGTM!The imports are correctly formatted and follow the naming conventions.
src/commands/secrets/cmds.ts (1)
17-58
: LGTM!The
registerLLMAPIKey
function is well-implemented and follows best practices. It handles different scenarios appropriately, such as:
- Allowing users to select an LLM provider using VSCode's
QuickPick
.- Handling the case when the user cancels the selection by showing a warning message.
- Constructing a secret key based on the selected provider.
- Checking if an API key already exists for the selected provider and prompting the user to update it if it exists.
- Prompting the user to enter a new API key if one doesn't exist.
- Handling the case when the user cancels the API key input by showing a warning message.
- Storing the API key using VSCode's secrets storage and showing a success message.
The function is modular, follows a clear naming convention for variables and the secret key, and handles errors appropriately.
src/common/vscodeapi.ts (1)
21-21
: LGTM!The import statement for
ExtensionContext
is necessary and approved.package.json (1)
300-305
: LGTM!The code changes are approved. The command name and title have been generalized as per the previous comment.
Feature: Register OpenAI API key
This pull request adds the ability to register an OpenAI API secret using VSCode's
SecretStorage
API.It adds one command to the command palette
Register OpenAI API Key
. You can use that to add/update your API key. It currently only supports OpenAI, but will be updated in the future to support other LLMs.To retrieve the API key, import the async function
getSecret
fromsrc/common/vscodeapi.ts
. You have to pass it anExtensionContext
object as an argument, along with a string for whichever secret you want (for OpenAI it will be'OPENAI_API_KEY'
). How exactly you get theExtensionContext
object is going to depend on how / where your code is executed.Summary by CodeRabbit
New Features
Bug Fixes
Documentation