Browse Source

Changes "Reset OpenAI Key" to "Reset AI Key"

Fixes the reset to work for the current provider
main
Eric Amodio 1 year ago
parent
commit
f51a8f7819
7 changed files with 65 additions and 73 deletions
  1. +3
    -3
      package.json
  2. +14
    -0
      src/ai/aiProviderService.ts
  3. +17
    -28
      src/ai/anthropicProvider.ts
  4. +16
    -22
      src/ai/openaiProvider.ts
  5. +1
    -17
      src/commands/generateCommitMessage.ts
  6. +11
    -0
      src/commands/resets.ts
  7. +3
    -3
      src/constants.ts

+ 3
- 3
package.json View File

@ -4607,8 +4607,8 @@
"category": "GitLens"
},
{
"command": "gitlens.resetOpenAIKey",
"title": "Reset Stored OpenAI Key",
"command": "gitlens.resetAIKey",
"title": "Reset Stored AI Key",
"category": "GitLens"
},
{
@ -10110,7 +10110,7 @@
"when": "gitlens:enabled && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders"
},
{
"command": "gitlens.resetOpenAIKey",
"command": "gitlens.resetAIKey",
"when": "gitlens:enabled"
}
],

+ 14
- 0
src/ai/aiProviderService.ts View File

@ -45,6 +45,10 @@ export class AIProviderService implements Disposable {
this._provider?.dispose();
}
get providerId() {
return this.provider?.id;
}
public async generateCommitMessage(
repoPath: string | Uri,
options?: { context?: string; progress?: ProgressOptions },
@ -125,6 +129,16 @@ export class AIProviderService implements Disposable {
}
return provider.explainChanges(commit.message, diff.contents);
}
reset() {
const { providerId } = this;
if (providerId == null) return;
void this.container.storage.deleteSecret(`gitlens.${providerId}.key`);
void this.container.storage.delete(`confirm:ai:tos:${providerId}`);
void this.container.storage.deleteWorkspace(`confirm:ai:tos:${providerId}`);
}
}
async function confirmAIProviderToS(provider: AIProvider, storage: Storage): Promise<boolean> {

+ 17
- 28
src/ai/anthropicProvider.ts View File

@ -55,19 +55,7 @@ export class AnthropicProvider implements AIProvider {
max_tokens_to_sample: 5000,
stop_sequences: ['\n\nHuman:'],
};
const rsp = await fetch('https://api.anthropic.com/v1/complete', {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
Client: 'anthropic-typescript/0.4.3',
'X-API-Key': apiKey,
},
method: 'POST',
body: JSON.stringify(request),
});
const rsp = await this.fetch(apiKey, request);
if (!rsp.ok) {
let json;
try {
@ -116,19 +104,7 @@ export class AnthropicProvider implements AIProvider {
stop_sequences: ['\n\nHuman:'],
};
const rsp = await fetch('https://api.anthropic.com/v1/complete', {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
Client: 'anthropic-typescript/0.4.3',
'X-API-Key': apiKey,
'anthropic-version': '2023-06-01',
},
method: 'POST',
body: JSON.stringify(request),
});
const rsp = await this.fetch(apiKey, request);
if (!rsp.ok) {
let json;
try {
@ -145,6 +121,19 @@ export class AnthropicProvider implements AIProvider {
const summary = data.completion.trim();
return summary;
}
private fetch(apiKey: string, request: AnthropicCompletionRequest) {
return fetch('https://api.anthropic.com/v1/complete', {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'X-API-Key': apiKey,
},
method: 'POST',
body: JSON.stringify(request),
});
}
}
async function getApiKey(storage: Storage): Promise<string | undefined> {
@ -165,7 +154,7 @@ async function getApiKey(storage: Storage): Promise {
disposables.push(
input.onDidHide(() => resolve(undefined)),
input.onDidChangeValue(value => {
if (value && !/sk-[a-zA-Z0-9-_]{32,}/.test(value)) {
if (value && !/(?:sk-)?[a-zA-Z0-9-_]{32,}/.test(value)) {
input.validationMessage = 'Please enter a valid Anthropic API key';
return;
}
@ -173,7 +162,7 @@ async function getApiKey(storage: Storage): Promise {
}),
input.onDidAccept(() => {
const value = input.value.trim();
if (!value || !/sk-[a-zA-Z0-9-_]{32,}/.test(value)) {
if (!value || !/(?:sk-)?[a-zA-Z0-9-_]{32,}/.test(value)) {
input.validationMessage = 'Please enter a valid Anthropic API key';
return;
}

+ 16
- 22
src/ai/openaiProvider.ts View File

@ -68,16 +68,7 @@ export class OpenAIProvider implements AIProvider {
content: `Write a meaningful commit message for the following code changes:\n\n${code}`,
});
const rsp = await fetch(this.url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(request),
});
const rsp = await this.fetch(apiKey, request);
if (!rsp.ok) {
debugger;
if (rsp.status === 429) {
@ -130,16 +121,7 @@ export class OpenAIProvider implements AIProvider {
],
};
const rsp = await fetch(this.url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(request),
});
const rsp = await this.fetch(apiKey, request);
if (!rsp.ok) {
debugger;
if (rsp.status === 404) {
@ -159,6 +141,18 @@ export class OpenAIProvider implements AIProvider {
const summary = data.choices[0].message.content.trim();
return summary;
}
private fetch(apiKey: string, request: OpenAIChatCompletionRequest) {
return fetch(this.url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(request),
});
}
}
async function getApiKey(storage: Storage): Promise<string | undefined> {
@ -179,7 +173,7 @@ async function getApiKey(storage: Storage): Promise {
disposables.push(
input.onDidHide(() => resolve(undefined)),
input.onDidChangeValue(value => {
if (value && !/(?:sk-)?[a-zA-Z0-9]{32}/.test(value)) {
if (value && !/(?:sk-)?[a-zA-Z0-9]{32,}/.test(value)) {
input.validationMessage = 'Please enter a valid OpenAI API key';
return;
}
@ -187,7 +181,7 @@ async function getApiKey(storage: Storage): Promise {
}),
input.onDidAccept(() => {
const value = input.value.trim();
if (!value || !/(?:sk-)?[a-zA-Z0-9]{32}/.test(value)) {
if (!value || !/(?:sk-)?[a-zA-Z0-9]{32,}/.test(value)) {
input.validationMessage = 'Please enter a valid OpenAI API key';
return;
}

+ 1
- 17
src/commands/generateCommitMessage.ts View File

@ -7,7 +7,7 @@ import { showGenericErrorMessage } from '../messages';
import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker';
import { command, executeCoreCommand } from '../system/command';
import { Logger } from '../system/logger';
import { ActiveEditorCommand, Command, getCommandUri } from './base';
import { ActiveEditorCommand, getCommandUri } from './base';
export interface GenerateCommitMessageCommandArgs {
repoPath?: string;
@ -59,19 +59,3 @@ export class GenerateCommitMessageCommand extends ActiveEditorCommand {
}
}
}
@command()
export class ResetOpenAIKeyCommand extends Command {
constructor(private readonly container: Container) {
super(Commands.ResetOpenAIKey);
}
execute() {
void this.container.storage.deleteSecret('gitlens.openai.key');
void this.container.storage.deleteWithPrefix('confirm:ai:tos');
void this.container.storage.deleteWorkspaceWithPrefix('confirm:ai:tos');
void this.container.storage.delete('confirm:sendToOpenAI');
void this.container.storage.deleteWorkspace('confirm:sendToOpenAI');
}
}

+ 11
- 0
src/commands/resets.ts View File

@ -7,6 +7,17 @@ import { configuration } from '../system/configuration';
import { Command } from './base';
@command()
export class ResetAIKeyCommand extends Command {
constructor(private readonly container: Container) {
super(Commands.ResetAIKey);
}
execute() {
this.container.ai.reset();
}
}
@command()
export class ResetAvatarCacheCommand extends Command {
constructor(private readonly container: Container) {
super(Commands.ResetAvatarCache);

+ 3
- 3
src/constants.ts View File

@ -222,7 +222,7 @@ export const enum Commands {
RefreshHover = 'gitlens.refreshHover',
RefreshTimelinePage = 'gitlens.timeline.refresh',
ResetAvatarCache = 'gitlens.resetAvatarCache',
ResetOpenAIKey = 'gitlens.resetOpenAIKey',
ResetAIKey = 'gitlens.resetAIKey',
ResetSuppressedWarnings = 'gitlens.resetSuppressedWarnings',
ResetTrackedUsage = 'gitlens.resetTrackedUsage',
ResetViewsLayout = 'gitlens.resetViewsLayout',
@ -714,7 +714,7 @@ export const enum SyncedStorageKeys {
}
export type DeprecatedGlobalStorage = {
/** @deprecated use `confirm:ai:send:openai` */
/** @deprecated use `confirm:ai:tos:${AIProviders}` */
'confirm:sendToOpenAI': boolean;
/** @deprecated */
'home:actions:completed': ('dismissed:welcome' | 'opened:scm')[];
@ -762,7 +762,7 @@ export type GlobalStorage = {
};
export type DeprecatedWorkspaceStorage = {
/** @deprecated use `confirm:ai:send:openai` */
/** @deprecated use `confirm:ai:tos:${AIProviders}` */
'confirm:sendToOpenAI': boolean;
/** @deprecated */
'graph:banners:dismissed': Record<string, boolean>;

Loading…
Cancel
Save