Skip to content

Commit 0d3fe77

Browse files
committed
Adds utility function to ensure AI features
1 parent e68f9b4 commit 0d3fe77

File tree

3 files changed

+57
-58
lines changed

3 files changed

+57
-58
lines changed

src/plus/ai/aiProviderService.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { assertsCommitHasFullDetails } from '../../git/utils/commit.utils';
3737
import { showAIModelPicker, showAIProviderPicker } from '../../quickpicks/aiModelPicker';
3838
import { Directive, isDirective } from '../../quickpicks/items/directive';
3939
import { configuration } from '../../system/-webview/configuration';
40-
import { getContext } from '../../system/-webview/context';
4140
import type { Storage } from '../../system/-webview/storage';
4241
import { debounce } from '../../system/function/debounce';
4342
import { map } from '../../system/iterable';
@@ -66,6 +65,7 @@ import type {
6665
PromptTemplateType,
6766
} from './models/promptTemplates';
6867
import type { AIChatMessage, AIProvider, AIRequestResult } from './models/provider';
68+
import { ensureAccess } from './utils/-webview/ai.utils';
6969
import { getLocalPromptTemplate, resolvePrompt } from './utils/-webview/prompt.utils';
7070

7171
export interface AIResult {
@@ -468,24 +468,8 @@ export class AIProviderService implements Disposable {
468468
return model;
469469
}
470470

471-
private async ensureAccess(): Promise<boolean> {
472-
const aiEnabled = configuration.get('ai.enabled');
473-
if (aiEnabled === false) {
474-
await window.showErrorMessage(`AI features have been disabled via GitLens settings.`);
475-
return false;
476-
}
477-
478-
const orgEnabled = getContext('gitlens:gk:organization:ai:enabled');
479-
if (orgEnabled === false) {
480-
await window.showErrorMessage(`AI features have been disabled for your organization.`);
481-
return false;
482-
}
483-
484-
return true;
485-
}
486-
487471
private async ensureFeatureAccess(feature: AIFeatures, source: Source): Promise<boolean> {
488-
if (!(await this.ensureAccess())) return false;
472+
if (!(await ensureAccess())) return false;
489473

490474
if (feature === 'generate-commitMessage') return true;
491475
if (

src/plus/ai/utils/-webview/ai.utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { AIProviders } from '../../../../constants.ai';
44
import type { Container } from '../../../../container';
55
import { createDirectiveQuickPickItem, Directive } from '../../../../quickpicks/items/directive';
66
import { configuration } from '../../../../system/-webview/configuration';
7+
import { getContext } from '../../../../system/-webview/context';
78
import { openSettingsEditor } from '../../../../system/-webview/vscode/editors';
89
import { formatNumeric } from '../../../../system/date';
910
import { getPossessiveForm, pluralize } from '../../../../system/string';
@@ -166,3 +167,54 @@ export function showPromptTruncationWarning(model: AIModel): void {
166167
export function isAzureUrl(url: string): boolean {
167168
return url.includes('.azure.com');
168169
}
170+
171+
export async function ensureAccess(options?: { showPicker?: boolean }): Promise<boolean> {
172+
const showPicker = options?.showPicker ?? false;
173+
174+
if (!getContext('gitlens:gk:organization:ai:enabled', true)) {
175+
if (showPicker) {
176+
await window.showQuickPick([{ label: 'OK' }], {
177+
title: 'AI is Disabled',
178+
placeHolder: 'GitLens AI features have been disabled by your GitKraken admin',
179+
canPickMany: false,
180+
});
181+
} else {
182+
await window.showErrorMessage(`AI features have been disabled by your GitKraken admin.`);
183+
}
184+
185+
return false;
186+
}
187+
188+
if (!configuration.get('ai.enabled')) {
189+
let reenable = false;
190+
if (showPicker) {
191+
const enable = { label: 'Re-enable AI Features' };
192+
const pick = await window.showQuickPick([{ label: 'OK' }, enable], {
193+
title: 'AI is Disabled',
194+
placeHolder: 'GitLens AI features have been disabled via settings',
195+
canPickMany: false,
196+
});
197+
if (pick === enable) {
198+
reenable = true;
199+
}
200+
} else {
201+
const enable = { title: 'Re-enable AI Features' };
202+
const result = await window.showErrorMessage(
203+
`AI features have been disabled via GitLens settings.`,
204+
enable,
205+
);
206+
if (result === enable) {
207+
reenable = true;
208+
}
209+
}
210+
211+
if (reenable) {
212+
await configuration.updateEffective('ai.enabled', true);
213+
return true;
214+
}
215+
216+
return false;
217+
}
218+
219+
return true;
220+
}

src/quickpicks/aiModelPicker.ts

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { QuickInputButtons, ThemeIcon, window } from 'vscode';
33
import type { AIProviders } from '../constants.ai';
44
import type { Container } from '../container';
55
import type { AIModel, AIModelDescriptor, AIProviderDescriptorWithConfiguration } from '../plus/ai/models/model';
6+
import { ensureAccess } from '../plus/ai/utils/-webview/ai.utils';
67
import { isSubscriptionPaidPlan } from '../plus/gk/utils/subscription.utils';
7-
import { configuration } from '../system/-webview/configuration';
8-
import { getContext } from '../system/-webview/context';
98
import { getQuickPickIgnoreFocusOut } from '../system/-webview/vscode';
109
import { getSettledValue } from '../system/promise';
1110
import { createQuickPickSeparator } from './items/common';
@@ -34,25 +33,7 @@ export async function showAIProviderPicker(
3433
container: Container,
3534
current: AIModelDescriptor | undefined,
3635
): Promise<ProviderQuickPickItem | undefined> {
37-
if (!configuration.get('ai.enabled')) {
38-
await window.showQuickPick([{ label: 'OK' }], {
39-
title: 'AI is Disabled',
40-
placeHolder: 'GitLens AI features have been disabled via settings',
41-
canPickMany: false,
42-
});
43-
44-
return undefined;
45-
}
46-
47-
if (!getContext('gitlens:gk:organization:ai:enabled', true)) {
48-
await window.showQuickPick([{ label: 'OK' }], {
49-
title: 'AI is Disabled',
50-
placeHolder: 'GitLens AI features have been disabled by your GitKraken admin',
51-
canPickMany: false,
52-
});
53-
54-
return undefined;
55-
}
36+
if (!(await ensureAccess({ showPicker: true }))) return undefined;
5637

5738
const [providersResult, modelResult, subscriptionResult] = await Promise.allSettled([
5839
container.ai.getProvidersConfiguration(),
@@ -150,25 +131,7 @@ export async function showAIModelPicker(
150131
provider: AIProviders,
151132
current?: AIModelDescriptor,
152133
): Promise<ModelQuickPickItem | Directive | undefined> {
153-
if (!configuration.get('ai.enabled')) {
154-
await window.showQuickPick([{ label: 'OK' }], {
155-
title: 'AI is Disabled',
156-
placeHolder: 'GitLens AI features have been disabled via settings',
157-
canPickMany: false,
158-
});
159-
160-
return undefined;
161-
}
162-
163-
if (!getContext('gitlens:gk:organization:ai:enabled', true)) {
164-
await window.showQuickPick([{ label: 'OK' }], {
165-
title: 'AI is Disabled',
166-
placeHolder: 'GitLens AI features have been disabled by your GitKraken admin',
167-
canPickMany: false,
168-
});
169-
170-
return undefined;
171-
}
134+
if (!(await ensureAccess({ showPicker: true }))) return undefined;
172135

173136
const models = (await container.ai.getModels(provider)) ?? [];
174137

0 commit comments

Comments
 (0)