Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions src/plus/ai/aiProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
AINoRequestDataError,
AuthenticationRequiredError,
CancellationError,
isCancellationError,
} from '../../errors';
import type { AIFeatures } from '../../features';
import { isAdvancedFeature } from '../../features';
Expand Down Expand Up @@ -1477,7 +1478,7 @@ export class AIProviderService implements Disposable {
const model = await this.getModel(undefined, source);
if (model == null || options?.cancellation?.isCancellationRequested) {
options?.generating?.cancel();
return undefined;
return 'cancelled';
}

const promise = this.sendRequestWithModel(
Expand Down Expand Up @@ -1522,7 +1523,7 @@ export class AIProviderService implements Disposable {
const model = await this.getModel(undefined, source);
if (model == null || options?.cancellation?.isCancellationRequested) {
options?.generating?.cancel();
return undefined;
return 'cancelled';
}

return this.sendRequestWithModel(
Expand Down Expand Up @@ -1618,7 +1619,24 @@ export class AIProviderService implements Disposable {
return 'cancelled';
}

const apiKey = await this._provider!.getApiKey(false);
let apiKey: string | undefined;
try {
apiKey = await this._provider!.getApiKey(false);
} catch (ex) {
if (isCancellationError(ex)) {
setLogScopeExit(scope, `model: ${model.provider.id}/${model.id}`, 'cancelled: user cancelled');
this.container.telemetry.sendEvent(
telementry.key,
{ ...telementry.data, failed: true, 'failed.reason': 'user-cancelled' },
source,
);

options?.generating?.cancel();
return 'cancelled';
}

throw ex;
}

if (cancellation.isCancellationRequested) {
setLogScopeExit(scope, `model: ${model.provider.id}/${model.id}`, 'cancelled: user cancelled');
Expand Down
11 changes: 9 additions & 2 deletions src/plus/ai/openAICompatibleProviderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fetch } from '@env/fetch';
import type { Role } from '../../@types/vsls';
import type { AIProviders } from '../../constants.ai';
import type { Container } from '../../container';
import { AIError, AIErrorReason, CancellationError } from '../../errors';
import { AIError, AIErrorReason, CancellationError, isCancellationError } from '../../errors';
import { getLoggableName, Logger } from '../../system/logger';
import { startLogScope } from '../../system/logger.scope';
import type { ServerConnection } from '../gk/serverConnection';
Expand Down Expand Up @@ -38,7 +38,14 @@ export abstract class OpenAICompatibleProviderBase<T extends AIProviders> implem
protected abstract readonly config: { keyUrl?: string; keyValidator?: RegExp };

async configured(silent: boolean): Promise<boolean> {
return (await this.getApiKey(silent)) != null;
try {
const apiKey = await this.getApiKey(silent);
return apiKey != null;
} catch (ex) {
if (isCancellationError(ex)) return false;

throw ex;
}
}

async getApiKey(silent: boolean): Promise<string | undefined> {
Expand Down
11 changes: 10 additions & 1 deletion src/plus/ai/openRouterProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetch } from '@env/fetch';
import { openRouterProviderDescriptor as provider } from '../../constants.ai';
import { isCancellationError } from '../../errors';
import type { AIActionType, AIModel } from './models/model';
import { OpenAICompatibleProviderBase } from './openAICompatibleProviderBase';

Expand All @@ -15,7 +16,15 @@ export class OpenRouterProvider extends OpenAICompatibleProviderBase<typeof prov
};

async getModels(): Promise<readonly AIModel<typeof provider.id>[]> {
const apiKey = await this.getApiKey(true);
let apiKey: string | undefined;
try {
apiKey = await this.getApiKey(true);
} catch (ex) {
if (isCancellationError(ex)) return [];

throw ex;
}

if (!apiKey) return [];

const url = 'https://openrouter.ai/api/v1/models';
Expand Down
11 changes: 9 additions & 2 deletions src/plus/ai/utils/-webview/ai.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Schemes } from '../../../../constants';
import type { AIProviders } from '../../../../constants.ai';
import type { Container } from '../../../../container';
import type { MarkdownContentMetadata } from '../../../../documents/markdown';
import { CancellationError } from '../../../../errors';
import { decodeGitLensRevisionUriAuthority } from '../../../../git/gitUri.authority';
import { createDirectiveQuickPickItem, Directive } from '../../../../quickpicks/items/directive';
import { configuration } from '../../../../system/-webview/configuration';
Expand All @@ -17,8 +18,8 @@ import { ensureAccountQuickPick } from '../../../gk/utils/-webview/acount.utils'
import type { AIResult, AIResultContext } from '../../aiProviderService';
import type { AIActionType, AIModel } from '../../models/model';

export function ensureAccount(container: Container, silent: boolean): Promise<boolean> {
return ensureAccountQuickPick(
export async function ensureAccount(container: Container, silent: boolean): Promise<boolean> {
const result = await ensureAccountQuickPick(
container,
createDirectiveQuickPickItem(Directive.Noop, undefined, {
label: 'Use AI-powered GitLens features like Generate Commit Message, Explain Commit, and more',
Expand All @@ -27,6 +28,12 @@ export function ensureAccount(container: Container, silent: boolean): Promise<bo
{ source: 'ai' },
silent,
);

if (!result && !silent) {
throw new CancellationError();
}

return result;
}

export function getActionName(action: AIActionType): string {
Expand Down
Loading