Skip to content

Commit 4eb0c8e

Browse files
committed
Fix #115: Remove hardcoded Anthropic API key check and add provider-specific error messages
1 parent 0fa80ab commit 4eb0c8e

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
11
export const getAnthropicApiKeyError = () => `
22
Error: ANTHROPIC_API_KEY environment variable is not set
33
4-
Before using MyCoder, you must have an ANTHROPIC_API_KEY specified either:
4+
Before using MyCoder with Anthropic models, you must have an ANTHROPIC_API_KEY specified either:
55
66
- As an environment variable, "export ANTHROPIC_API_KEY=[your-api-key]" or
77
- In a .env file in the folder you run "mycoder" from
88
99
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
19+
- In a .env file in the folder you run "mycoder" from
20+
21+
Get an API key from https://platform.openai.com/api-keys
22+
For setup instructions, visit: https://mycoder.ai/docs/getting-started/openai
23+
`;
24+
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
1047
`;

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
Logger,
88
getTools,
99
getAnthropicApiKeyError,
10+
getOpenAIApiKeyError,
11+
getXAIApiKeyError,
12+
getMistralApiKeyError,
1013
userPrompt,
1114
LogLevel,
1215
subAgentTool,
@@ -102,25 +105,16 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
102105
userModelProvider === 'openai' &&
103106
!process.env.OPENAI_API_KEY
104107
) {
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-
);
108+
logger.error(getOpenAIApiKeyError());
109109
throw new Error('OpenAI API key not found');
110110
} 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-
);
111+
logger.error(getXAIApiKeyError());
115112
throw new Error('xAI API key not found');
116113
} else if (
117114
userModelProvider === 'mistral' &&
118115
!process.env.MISTRAL_API_KEY
119116
) {
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-
);
117+
logger.error(getMistralApiKeyError());
124118
throw new Error('Mistral API key not found');
125119
}
126120
// No API key check needed for Ollama as it uses a local server

0 commit comments

Comments
 (0)