Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
"peerDependencies": {
"@langchain/core": ">=1.1.31"
},
"devDependencies": {
"optionalDependencies": {
"@langchain/anthropic": "^1.0.0",
"@langchain/core": "^1.1.31",
"@langchain/google-genai": "^2.1.24",
"@langchain/openai": "^1.2.12",
"@langchain/openai": "^1.2.12"
},
"devDependencies": {
"@langchain/core": "^1.1.31",
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.16",
"@types/merge-images": "^1.2.4",
Expand Down
43 changes: 40 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ import {
} from "@/types";
import { BrowserAgentError } from "@/agent/error";

/**
* Dynamically load a provider SDK and surface a clear, actionable error if
* it's missing. The provider packages are declared as
* `optionalDependencies`, so they normally ship with the CLI — but a user
* who ran `npm install --omit=optional` (or whose install was interrupted)
* may not have them. In that case, tell them exactly which package to
* install instead of printing a raw `MODULE_NOT_FOUND` stack.
*/
async function loadProvider<T>(
packageName: string,
providerLabel: string
): Promise<T> {
try {
return (await import(packageName)) as T;
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const code = (err as any)?.code;
if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") {
console.error(
chalk.red(
`${providerLabel} provider is not installed.\n` +
`Install it and retry:\n\n` +
` npm install -g ${packageName}\n`
)
);
process.exit(1);
}
throw err;
}
}

/**
* Select an LLM based on environment variables. Providers are checked in
* priority order: OpenAI, Google, Anthropic. Per-provider model is
Expand All @@ -31,23 +62,29 @@ import { BrowserAgentError } from "@/agent/error";
*/
async function createDefaultLlm(): Promise<BaseChatModel | undefined> {
if (process.env.OPENAI_API_KEY) {
const { ChatOpenAI } = await import("@langchain/openai");
const { ChatOpenAI } = await loadProvider<
typeof import("@langchain/openai")
>("@langchain/openai", "OpenAI");
return new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: process.env.OPENAI_MODEL ?? "gpt-4.1-mini",
temperature: 0,
}) as unknown as BaseChatModel;
}
if (process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY) {
const { ChatGoogleGenerativeAI } = await import("@langchain/google-genai");
const { ChatGoogleGenerativeAI } = await loadProvider<
typeof import("@langchain/google-genai")
>("@langchain/google-genai", "Google Gemini");
return new ChatGoogleGenerativeAI({
apiKey: process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY,
model: process.env.GEMINI_MODEL ?? "gemini-2.5-flash",
temperature: 0,
}) as unknown as BaseChatModel;
}
if (process.env.ANTHROPIC_API_KEY) {
const { ChatAnthropic } = await import("@langchain/anthropic");
const { ChatAnthropic } = await loadProvider<
typeof import("@langchain/anthropic")
>("@langchain/anthropic", "Anthropic");
return new ChatAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model:
Expand Down
Loading