Skip to content

Commit 4677632

Browse files
authored
feat: add OpenAI and Anthropic support to compiler (#1482)
1 parent 0d933f8 commit 4677632

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

packages/compiler/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
"vitest": "^2.1.4"
4646
},
4747
"dependencies": {
48+
"@ai-sdk/anthropic": "^1.0.11",
4849
"@ai-sdk/google": "^1.2.19",
4950
"@ai-sdk/groq": "^1.2.3",
5051
"@ai-sdk/mistral": "^1.2.8",
52+
"@ai-sdk/openai": "^1.3.22",
5153
"@babel/generator": "^7.26.5",
5254
"@babel/parser": "^7.26.7",
5355
"@babel/traverse": "^7.27.4",

packages/compiler/src/lib/lcp/api/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { createGoogleGenerativeAI } from "@ai-sdk/google";
33
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
44
import { createOllama } from "ollama-ai-provider";
55
import { createMistral } from "@ai-sdk/mistral";
6+
import { createOpenAI } from "@ai-sdk/openai";
7+
import { createAnthropic } from "@ai-sdk/anthropic";
68
import { generateText } from "ai";
79
import { LingoDotDevEngine } from "@lingo.dev/_sdk";
810
import { DictionarySchema } from "../schema";
@@ -20,6 +22,10 @@ import {
2022
getOpenRouterKeyFromEnv,
2123
getMistralKey,
2224
getMistralKeyFromEnv,
25+
getOpenAIKey,
26+
getOpenAIKeyFromEnv,
27+
getAnthropicKey,
28+
getAnthropicKeyFromEnv,
2329
getLingoDotDevKeyFromEnv,
2430
getLingoDotDevKey,
2531
} from "../../../utils/llm-api-key";
@@ -383,9 +389,49 @@ export class LCPAPI {
383389
return createMistral({ apiKey: mistralKey })(modelId);
384390
}
385391

392+
case "openai": {
393+
// Specific check for CI/CD or Docker missing OpenAI key
394+
if (isRunningInCIOrDocker()) {
395+
const openaiFromEnv = getOpenAIKeyFromEnv();
396+
if (!openaiFromEnv) {
397+
this._failMissingLLMKeyCi(providerId);
398+
}
399+
}
400+
const openaiKey = getOpenAIKey();
401+
if (!openaiKey) {
402+
throw new Error(
403+
"⚠️ OpenAI API key not found. Please set OPENAI_API_KEY environment variable or configure it user-wide.",
404+
);
405+
}
406+
console.log(
407+
`Creating OpenAI client for ${targetLocale} using model ${modelId}`,
408+
);
409+
return createOpenAI({ apiKey: openaiKey })(modelId);
410+
}
411+
412+
case "anthropic": {
413+
// Specific check for CI/CD or Docker missing Anthropic key
414+
if (isRunningInCIOrDocker()) {
415+
const anthropicFromEnv = getAnthropicKeyFromEnv();
416+
if (!anthropicFromEnv) {
417+
this._failMissingLLMKeyCi(providerId);
418+
}
419+
}
420+
const anthropicKey = getAnthropicKey();
421+
if (!anthropicKey) {
422+
throw new Error(
423+
"⚠️ Anthropic API key not found. Please set ANTHROPIC_API_KEY environment variable or configure it user-wide.",
424+
);
425+
}
426+
console.log(
427+
`Creating Anthropic client for ${targetLocale} using model ${modelId}`,
428+
);
429+
return createAnthropic({ apiKey: anthropicKey })(modelId);
430+
}
431+
386432
default: {
387433
throw new Error(
388-
`⚠️ Provider "${providerId}" for locale "${targetLocale}" is not supported. Only "groq", "google", "openrouter", "ollama", and "mistral" providers are supported at the moment.`,
434+
`⚠️ Provider "${providerId}" for locale "${targetLocale}" is not supported. Only "groq", "google", "openrouter", "ollama", "mistral", "openai", and "anthropic" providers are supported at the moment.`,
389435
);
390436
}
391437
}

packages/compiler/src/lib/lcp/api/provider-details.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe("provider-details", () => {
1111
"openrouter",
1212
"ollama",
1313
"mistral",
14+
"openai",
15+
"anthropic",
1416
"lingo.dev",
1517
]);
1618
});

packages/compiler/src/lib/lcp/api/provider-details.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export const providerDetails: Record<
5959
getKeyLink: "https://console.mistral.ai",
6060
docsLink: "https://docs.mistral.ai",
6161
},
62+
openai: {
63+
name: "OpenAI",
64+
apiKeyEnvVar: "OPENAI_API_KEY",
65+
apiKeyConfigKey: "llm.openaiApiKey",
66+
getKeyLink: "https://platform.openai.com/api-keys",
67+
docsLink: "https://platform.openai.com/docs/guides/error-codes",
68+
},
69+
anthropic: {
70+
name: "Anthropic",
71+
apiKeyEnvVar: "ANTHROPIC_API_KEY",
72+
apiKeyConfigKey: "llm.anthropicApiKey",
73+
getKeyLink: "https://console.anthropic.com/",
74+
docsLink: "https://docs.anthropic.com/en/api/errors",
75+
},
6276
"lingo.dev": {
6377
name: "Lingo.dev",
6478
apiKeyEnvVar: "LINGODOTDEV_API_KEY",

packages/compiler/src/utils/llm-api-key.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,27 @@ export function getMistralKeyFromRc() {
8282
export function getMistralKeyFromEnv() {
8383
return getKeyFromEnv("MISTRAL_API_KEY");
8484
}
85+
86+
export function getOpenAIKey() {
87+
return getOpenAIKeyFromEnv() || getOpenAIKeyFromRc();
88+
}
89+
90+
export function getOpenAIKeyFromRc() {
91+
return getKeyFromRc("llm.openaiApiKey");
92+
}
93+
94+
export function getOpenAIKeyFromEnv() {
95+
return getKeyFromEnv("OPENAI_API_KEY");
96+
}
97+
98+
export function getAnthropicKey() {
99+
return getAnthropicKeyFromEnv() || getAnthropicKeyFromRc();
100+
}
101+
102+
export function getAnthropicKeyFromRc() {
103+
return getKeyFromRc("llm.anthropicApiKey");
104+
}
105+
106+
export function getAnthropicKeyFromEnv() {
107+
return getKeyFromEnv("ANTHROPIC_API_KEY");
108+
}

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)