Skip to content

Commit d3e2ef9

Browse files
committed
Refactor: Use provider config map for API key checks and error messages
1 parent 4eb0c8e commit d3e2ef9

File tree

2 files changed

+63
-65
lines changed

2 files changed

+63
-65
lines changed

packages/agent/src/utils/errors.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,54 @@
1-
export const getAnthropicApiKeyError = () => `
2-
Error: ANTHROPIC_API_KEY environment variable is not set
3-
4-
Before using MyCoder with Anthropic models, you must have an ANTHROPIC_API_KEY specified either:
5-
6-
- As an environment variable, "export ANTHROPIC_API_KEY=[your-api-key]" or
7-
- In a .env file in the folder you run "mycoder" from
8-
9-
Get an API key from https://www.anthropic.com/api
10-
For setup instructions, visit: https://mycoder.ai/docs/getting-started/anthropic
11-
`;
12-
13-
export const getOpenAIApiKeyError = () => `
14-
Error: OPENAI_API_KEY environment variable is not set
15-
16-
Before using MyCoder with OpenAI models, you must have an OPENAI_API_KEY specified either:
17-
18-
- As an environment variable, "export OPENAI_API_KEY=[your-api-key]" or
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+
};
25+
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];
33+
34+
if (!config) {
35+
return `Unknown provider: ${provider}`;
36+
}
37+
38+
const { keyName, docsUrl } = config;
39+
40+
return `
41+
Error: ${keyName} environment variable is not set
42+
43+
Before using MyCoder with ${provider} models, you must have a ${keyName} specified either:
44+
45+
- As an environment variable, "export ${keyName}=[your-api-key]" or
1946
- In a .env file in the folder you run "mycoder" from
2047
21-
Get an API key from https://platform.openai.com/api-keys
22-
For setup instructions, visit: https://mycoder.ai/docs/getting-started/openai
48+
For setup instructions, visit: ${docsUrl}
2349
`;
50+
};
2451

25-
export const getXAIApiKeyError = () => `
26-
Error: XAI_API_KEY environment variable is not set
27-
28-
Before using MyCoder with xAI models, you must have an XAI_API_KEY specified either:
29-
30-
- As an environment variable, "export XAI_API_KEY=[your-api-key]" or
31-
- In a .env file in the folder you run "mycoder" from
32-
33-
Get an API key from https://platform.xai.com
34-
For setup instructions, visit: https://mycoder.ai/docs/getting-started/xai
35-
`;
36-
37-
export const getMistralApiKeyError = () => `
38-
Error: MISTRAL_API_KEY environment variable is not set
39-
40-
Before using MyCoder with Mistral models, you must have a MISTRAL_API_KEY specified either:
41-
42-
- As an environment variable, "export MISTRAL_API_KEY=[your-api-key]" or
43-
- In a .env file in the folder you run "mycoder" from
44-
45-
Get an API key from https://console.mistral.ai/api-keys/
46-
For setup instructions, visit: https://mycoder.ai/docs/getting-started/mistral
47-
`;
52+
// Legacy function for backward compatibility
53+
export const getAnthropicApiKeyError = () =>
54+
getProviderApiKeyError('anthropic');

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import {
66
toolAgent,
77
Logger,
88
getTools,
9-
getAnthropicApiKeyError,
10-
getOpenAIApiKeyError,
11-
getXAIApiKeyError,
12-
getMistralApiKeyError,
9+
getProviderApiKeyError,
10+
providerConfig,
1311
userPrompt,
1412
LogLevel,
1513
subAgentTool,
@@ -98,24 +96,17 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
9896
const userModelName = argv.modelName || userConfig.modelName;
9997

10098
// Early API key check based on model provider
101-
if (userModelProvider === 'anthropic' && !process.env.ANTHROPIC_API_KEY) {
102-
logger.error(getAnthropicApiKeyError());
103-
throw new Error('Anthropic API key not found');
104-
} else if (
105-
userModelProvider === 'openai' &&
106-
!process.env.OPENAI_API_KEY
107-
) {
108-
logger.error(getOpenAIApiKeyError());
109-
throw new Error('OpenAI API key not found');
110-
} else if (userModelProvider === 'xai' && !process.env.XAI_API_KEY) {
111-
logger.error(getXAIApiKeyError());
112-
throw new Error('xAI API key not found');
113-
} else if (
114-
userModelProvider === 'mistral' &&
115-
!process.env.MISTRAL_API_KEY
116-
) {
117-
logger.error(getMistralApiKeyError());
118-
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+
}
119110
}
120111
// No API key check needed for Ollama as it uses a local server
121112

0 commit comments

Comments
 (0)