-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathanthropic.ts
More file actions
58 lines (51 loc) · 2 KB
/
anthropic.ts
File metadata and controls
58 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2026 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as positron from 'positron';
import { ANTHROPIC_API_VERSION, KEY_VALIDATION_TIMEOUT_MS } from '../constants';
class ApiKeyValidationError extends Error {
constructor(message: string) {
super(message);
this.name = 'ApiKeyValidationError';
}
}
export async function validateAnthropicApiKey(apiKey: string, config: positron.ai.LanguageModelConfig): Promise<void> {
const rawBaseUrl = (config.baseUrl ?? 'https://api.anthropic.com')
.replace(/\/v1\/?$/, '')
.replace(/\/+$/, '');
const modelsEndpoint = `${rawBaseUrl}/v1/models`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), KEY_VALIDATION_TIMEOUT_MS);
try {
const response = await fetch(modelsEndpoint, {
method: 'GET',
headers: {
'x-api-key': apiKey,
'anthropic-version': ANTHROPIC_API_VERSION,
},
signal: controller.signal,
});
if (response.ok) {
return;
}
if (response.status === 401 || response.status === 403) {
throw new ApiKeyValidationError(vscode.l10n.t('Invalid Anthropic API key'));
}
throw new ApiKeyValidationError(vscode.l10n.t(
'Unable to validate Anthropic API key (HTTP {0})',
String(response.status)
));
} catch (err) {
if (err instanceof Error && err.name === 'AbortError') {
throw new ApiKeyValidationError(vscode.l10n.t('Could not validate Anthropic API key within {0} seconds', String(KEY_VALIDATION_TIMEOUT_MS / 1000)));
}
if (err instanceof ApiKeyValidationError) {
throw err;
}
throw new ApiKeyValidationError(vscode.l10n.t('Could not validate Anthropic API key. Check your network connection and try again.'));
} finally {
clearTimeout(timeout);
}
}