Skip to content

Commit e048fc7

Browse files
authored
Merge pull request #116 from drivecore/fix/115-provider-specific-api-key-check
Fix #115: Remove hardcoded Anthropic API key check
2 parents 0fa80ab + 295ae08 commit e048fc7

File tree

3 files changed

+73
-38
lines changed

3 files changed

+73
-38
lines changed

packages/agent/src/core/toolAgent/toolAgentCore.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { CoreMessage, ToolSet, generateText, tool as makeTool } from 'ai';
22

3-
import { getAnthropicApiKeyError } from '../../utils/errors.js';
4-
53
import { DEFAULT_CONFIG } from './config.js';
64
import {
75
addCacheControlToMessages,
@@ -30,9 +28,6 @@ export const toolAgent = async (
3028

3129
let interactions = 0;
3230

33-
const apiKey = process.env.ANTHROPIC_API_KEY;
34-
if (!apiKey) throw new Error(getAnthropicApiKeyError());
35-
3631
const messages: CoreMessage[] = [
3732
{
3833
role: 'user',

packages/agent/src/utils/errors.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1-
export const getAnthropicApiKeyError = () => `
2-
Error: ANTHROPIC_API_KEY environment variable is not set
1+
// Provider configuration map
2+
export const providerConfig: Record<
3+
string,
4+
{ keyName: string; docsUrl: string } | undefined
5+
> = {
6+
anthropic: {
7+
keyName: 'ANTHROPIC_API_KEY',
8+
docsUrl: 'https://mycoder.ai/docs/getting-started/anthropic',
9+
},
10+
openai: {
11+
keyName: 'OPENAI_API_KEY',
12+
docsUrl: 'https://mycoder.ai/docs/getting-started/openai',
13+
},
14+
xai: {
15+
keyName: 'XAI_API_KEY',
16+
docsUrl: 'https://mycoder.ai/docs/getting-started/xai',
17+
},
18+
mistral: {
19+
keyName: 'MISTRAL_API_KEY',
20+
docsUrl: 'https://mycoder.ai/docs/getting-started/mistral',
21+
},
22+
// No API key needed for ollama as it uses a local server
23+
ollama: undefined,
24+
};
325

4-
Before using MyCoder, you must have an ANTHROPIC_API_KEY specified either:
26+
/**
27+
* Generates a provider-specific API key error message
28+
* @param provider The LLM provider name
29+
* @returns Error message with provider-specific instructions
30+
*/
31+
export const getProviderApiKeyError = (provider: string): string => {
32+
const config = providerConfig[provider];
533

6-
- As an environment variable, "export ANTHROPIC_API_KEY=[your-api-key]" or
34+
if (!config) {
35+
return `Unknown provider: ${provider}`;
36+
}
37+
38+
const { keyName, docsUrl } = config;
39+
const platform = process.platform;
40+
let osSpecificInstructions = '';
41+
42+
if (platform === 'win32') {
43+
osSpecificInstructions = `- Using the windows command prompt, "setx ${keyName}=[your-api-key]"`;
44+
} else if (platform === 'darwin' || platform === 'linux') {
45+
osSpecificInstructions = `- As an environment variable, "export ${keyName}=[your-api-key]"`;
46+
} else {
47+
osSpecificInstructions = `- As an environment variable (platform-specific command)`;
48+
}
49+
50+
return `
51+
Error: ${keyName} environment variable is not set
52+
53+
Before using MyCoder with ${provider} models, you must have a ${keyName} specified.
54+
55+
You can set it via:
56+
${osSpecificInstructions}
757
- In a .env file in the folder you run "mycoder" from
858
9-
Get an API key from https://www.anthropic.com/api
59+
For setup instructions, visit: ${docsUrl}
1060
`;
61+
};
62+
63+
// Legacy function for backward compatibility
64+
export const getAnthropicApiKeyError = () =>
65+
getProviderApiKeyError('anthropic');

packages/cli/src/commands/$default.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
toolAgent,
77
Logger,
88
getTools,
9-
getAnthropicApiKeyError,
9+
getProviderApiKeyError,
10+
providerConfig,
1011
userPrompt,
1112
LogLevel,
1213
subAgentTool,
@@ -95,33 +96,17 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
9596
const userModelName = argv.modelName || userConfig.modelName;
9697

9798
// Early API key check based on model provider
98-
if (userModelProvider === 'anthropic' && !process.env.ANTHROPIC_API_KEY) {
99-
logger.error(getAnthropicApiKeyError());
100-
throw new Error('Anthropic API key not found');
101-
} else if (
102-
userModelProvider === 'openai' &&
103-
!process.env.OPENAI_API_KEY
104-
) {
105-
logger.error(
106-
'No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.',
107-
'You can get an API key from https://platform.openai.com/api-keys',
108-
);
109-
throw new Error('OpenAI API key not found');
110-
} else if (userModelProvider === 'xai' && !process.env.XAI_API_KEY) {
111-
logger.error(
112-
'No xAI API key found. Please set the XAI_API_KEY environment variable.',
113-
'You can get an API key from https://platform.xai.com',
114-
);
115-
throw new Error('xAI API key not found');
116-
} else if (
117-
userModelProvider === 'mistral' &&
118-
!process.env.MISTRAL_API_KEY
119-
) {
120-
logger.error(
121-
'No Mistral API key found. Please set the MISTRAL_API_KEY environment variable.',
122-
'You can get an API key from https://console.mistral.ai/api-keys/',
123-
);
124-
throw new Error('Mistral API key not found');
99+
const providerSettings =
100+
providerConfig[userModelProvider as keyof typeof providerConfig];
101+
102+
if (providerSettings) {
103+
const { keyName } = providerSettings;
104+
const apiKey = process.env[keyName];
105+
106+
if (!apiKey) {
107+
logger.error(getProviderApiKeyError(userModelProvider));
108+
throw new Error(`${userModelProvider} API key not found`);
109+
}
125110
}
126111
// No API key check needed for Ollama as it uses a local server
127112

0 commit comments

Comments
 (0)