Skip to content

add new models #867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/solid-emus-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

Added support for new Anthropic computer-use models. Also added support for passing in new Anthropic models into Stagehand via the legacy (non AI SDK) format until we fully migrate.
11 changes: 10 additions & 1 deletion lib/agent/AgentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ import {

// Map model names to their provider types
const modelToAgentProviderMap: Record<string, AgentType> = {
// OpenAI models
"computer-use-preview": "openai",
"computer-use-preview-2025-03-11": "openai",
"claude-3-7-sonnet-latest": "anthropic",
// Anthropic models
"claude-sonnet-4-0": "anthropic",
"claude-sonnet-4-20250514": "anthropic",
"claude-opus-4-0": "anthropic",
"claude-opus-4-20250514": "anthropic",
"claude-3-7-sonnet-latest": "anthropic",
"claude-3-7-sonnet-20250219": "anthropic",
"claude-3-5-sonnet-latest": "anthropic",
"claude-3-5-sonnet-20241022": "anthropic",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider sorting model versions chronologically - 20240620 appears after 20241022

"claude-3-5-sonnet-20240620": "anthropic",
};

/**
Expand Down
60 changes: 47 additions & 13 deletions lib/agent/AnthropicCUAClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Anthropic from "@anthropic-ai/sdk";
import Anthropic, { ClientOptions } from "@anthropic-ai/sdk";
import { LogLine } from "@/types/log";
import {
AgentAction,
Expand Down Expand Up @@ -35,7 +35,7 @@ export class AnthropicCUAClient extends AgentClient {
type: AgentType,
modelName: string,
userProvidedInstructions?: string,
clientOptions?: Record<string, unknown>,
clientOptions?: ClientOptions,
) {
super(type, modelName, userProvidedInstructions);

Expand All @@ -44,21 +44,13 @@ export class AnthropicCUAClient extends AgentClient {
(clientOptions?.apiKey as string) || process.env.ANTHROPIC_API_KEY || "";
this.baseURL = (clientOptions?.baseURL as string) || undefined;

// Get thinking budget if specified
if (
clientOptions?.thinkingBudget &&
typeof clientOptions.thinkingBudget === "number"
) {
this.thinkingBudget = clientOptions.thinkingBudget;
}

Comment on lines -47 to -54
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed since the key did not exist on the client options type

// Store client options for reference
this.clientOptions = {
apiKey: this.apiKey,
};

if (this.baseURL) {
this.clientOptions.baseUrl = this.baseURL;
this.clientOptions.baseURL = this.baseURL;
}

// Initialize the Anthropic client
Expand Down Expand Up @@ -406,21 +398,26 @@ export class AnthropicCUAClient extends AgentClient {
? { type: "enabled" as const, budget_tokens: this.thinkingBudget }
: undefined;

// Determine the appropriate computer type and beta flag based on model
const { computerType, betaFlag } = this.getComputerUseConfigForModel(
this.modelName,
);

// Create the request parameters
const requestParams: Record<string, unknown> = {
model: this.modelName,
max_tokens: 4096,
messages: messages,
tools: [
{
type: "computer_20250124", // Use the latest version for Claude 3.7 Sonnet
type: computerType,
name: "computer",
display_width_px: this.currentViewport.width,
display_height_px: this.currentViewport.height,
display_number: 1,
},
],
betas: ["computer-use-2025-01-24"],
betas: [betaFlag],
};

// Add system parameter if provided
Expand Down Expand Up @@ -474,6 +471,43 @@ export class AnthropicCUAClient extends AgentClient {
}
}

/**
* Get the appropriate computer type and beta flag for a given model
*/
private getComputerUseConfigForModel(modelName: string): {
computerType: string;
betaFlag: string;
} {
// Claude 3.5 models use computer_20241022 with computer-use-2024-10-22
if (
modelName.includes("claude-3-5-sonnet") ||
modelName.includes("claude-3-5")
) {
return {
computerType: "computer_20241022",
betaFlag: "computer-use-2024-10-22",
};
}

// Claude 4 models and Claude Sonnet 3.7 use computer_20250124 with computer-use-2025-01-24
if (
modelName.includes("claude-4") ||
modelName.includes("claude-3-7") ||
modelName.includes("claude-3-7-sonnet")
) {
return {
computerType: "computer_20250124",
betaFlag: "computer-use-2025-01-24",
};
}

// Default fallback for other models
return {
computerType: "computer_20250124",
betaFlag: "computer-use-2025-01-24",
};
}

async takeAction(
toolUseItems: ToolUseItem[],
logger: (message: LogLine) => void,
Expand Down
30 changes: 18 additions & 12 deletions lib/agent/OpenAICUAClient.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import OpenAI from "openai";
import { LogLine } from "../../types/log";
import {
AgentAction,
AgentExecutionOptions,
AgentResult,
AgentType,
AgentExecutionOptions,
ResponseInputItem,
ResponseItem,
ComputerCallItem,
FunctionCallItem,
ResponseInputItem,
ResponseItem,
} from "@/types/agent";
import { AgentClient } from "./AgentClient";
import { AgentScreenshotProviderError } from "@/types/stagehandErrors";
import OpenAI, { ClientOptions } from "openai";
import { LogLine } from "../../types/log";
import { AgentClient } from "./AgentClient";

/**
* Client for OpenAI's Computer Use Assistant API
Expand All @@ -34,7 +34,7 @@ export class OpenAICUAClient extends AgentClient {
type: AgentType,
modelName: string,
userProvidedInstructions?: string,
clientOptions?: Record<string, unknown>,
clientOptions?: ClientOptions,
) {
super(type, modelName, userProvidedInstructions);

Expand All @@ -43,15 +43,21 @@ export class OpenAICUAClient extends AgentClient {
(clientOptions?.apiKey as string) || process.env.OPENAI_API_KEY || "";
this.organization =
(clientOptions?.organization as string) || process.env.OPENAI_ORG;
this.baseURL = (clientOptions?.baseURL as string) || undefined;

// Get environment if specified
if (
clientOptions?.environment &&
typeof clientOptions.environment === "string"
) {
this.environment = clientOptions.environment;
Comment on lines -48 to -52
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed since the environment key did not exist on the client options type

// Store client options for reference
this.clientOptions = {
apiKey: this.apiKey,
};

if (this.baseURL) {
this.clientOptions.baseURL = this.baseURL;
}

// Initialize the OpenAI client
this.client = new OpenAI(this.clientOptions);

// Store client options for reference
this.clientOptions = {
apiKey: this.apiKey,
Expand Down
30 changes: 17 additions & 13 deletions lib/llm/LLMProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { AISDKCustomProvider, AISDKProvider } from "@/types/llm";
import {
UnsupportedAISDKModelProviderError,
UnsupportedModelError,
UnsupportedModelProviderError,
} from "@/types/stagehandErrors";
import { anthropic, createAnthropic } from "@ai-sdk/anthropic";
import { azure, createAzure } from "@ai-sdk/azure";
import { cerebras, createCerebras } from "@ai-sdk/cerebras";
import { createDeepSeek, deepseek } from "@ai-sdk/deepseek";
import { createGoogleGenerativeAI, google } from "@ai-sdk/google";
import { createGroq, groq } from "@ai-sdk/groq";
import { createMistral, mistral } from "@ai-sdk/mistral";
import { createOpenAI, openai } from "@ai-sdk/openai";
import { createPerplexity, perplexity } from "@ai-sdk/perplexity";
import { createTogetherAI, togetherai } from "@ai-sdk/togetherai";
import { createXai, xai } from "@ai-sdk/xai";
import { ollama } from "ollama-ai-provider";
import { LogLine } from "../../types/log";
import {
AvailableModel,
Expand All @@ -17,19 +30,6 @@ import { GoogleClient } from "./GoogleClient";
import { GroqClient } from "./GroqClient";
import { LLMClient } from "./LLMClient";
import { OpenAIClient } from "./OpenAIClient";
import { openai, createOpenAI } from "@ai-sdk/openai";
import { anthropic, createAnthropic } from "@ai-sdk/anthropic";
import { google, createGoogleGenerativeAI } from "@ai-sdk/google";
import { xai, createXai } from "@ai-sdk/xai";
import { azure, createAzure } from "@ai-sdk/azure";
import { groq, createGroq } from "@ai-sdk/groq";
import { cerebras, createCerebras } from "@ai-sdk/cerebras";
import { togetherai, createTogetherAI } from "@ai-sdk/togetherai";
import { mistral, createMistral } from "@ai-sdk/mistral";
import { deepseek, createDeepSeek } from "@ai-sdk/deepseek";
import { perplexity, createPerplexity } from "@ai-sdk/perplexity";
import { ollama } from "ollama-ai-provider";
import { AISDKProvider, AISDKCustomProvider } from "@/types/llm";

const AISDKProviders: Record<string, AISDKProvider> = {
openai,
Expand Down Expand Up @@ -80,6 +80,10 @@ const modelToProviderMap: { [key in AvailableModel]: ModelProvider } = {
"claude-3-5-sonnet-20241022": "anthropic",
"claude-3-7-sonnet-20250219": "anthropic",
"claude-3-7-sonnet-latest": "anthropic",
"claude-opus-4-0": "anthropic",
"claude-opus-4-20250514": "anthropic",
"claude-sonnet-4-0": "anthropic",
"claude-sonnet-4-20250514": "anthropic",
"cerebras-llama-3.3-70b": "cerebras",
"cerebras-llama-3.1-8b": "cerebras",
"groq-llama-3.3-70b-versatile": "groq",
Expand Down