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 1 commit
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
3 changes: 2 additions & 1 deletion lib/agent/AgentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AgentType,
AgentExecutionOptions,
} from "@/types/agent";
import { ClientOptions } from "@anthropic-ai/sdk";
Copy link
Contributor

Choose a reason for hiding this comment

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

style: importing ClientOptions from Anthropic SDK when this is a base class that should be implementation-agnostic. Consider moving specific SDK types to the implementing classes.

Suggested change
import { ClientOptions } from "@anthropic-ai/sdk";
import type { LLMClientOptions } from "@/types/llm";


/**
* Abstract base class for agent clients
Expand All @@ -12,7 +13,7 @@ import {
export abstract class AgentClient {
public type: AgentType;
public modelName: string;
public clientOptions: Record<string, unknown>;
public clientOptions: ClientOptions;
public userProvidedInstructions?: string;

constructor(
Expand Down
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
49 changes: 46 additions & 3 deletions lib/agent/AnthropicCUAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class AnthropicCUAClient extends AgentClient {
// Process client options
this.apiKey =
(clientOptions?.apiKey as string) || process.env.ANTHROPIC_API_KEY || "";
console.log("USING API KEY", this.apiKey);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Logging API keys is a security risk. Consider removing this line or masking the key

Suggested change
console.log("USING API KEY", this.apiKey);
// this.apiKey is set;

this.baseURL = (clientOptions?.baseURL as string) || undefined;

// Get thinking budget if specified
Expand All @@ -58,7 +59,7 @@ export class AnthropicCUAClient extends AgentClient {
};

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

// Initialize the Anthropic client
Expand Down Expand Up @@ -406,21 +407,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 +480,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 4 models and Claude Sonnet 3.7 use computer_20250124 with computer-use-2025-01-24
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
22 changes: 10 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,13 +43,11 @@ 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;
if (this.baseURL) {
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

this.clientOptions.baseURL = this.baseURL;
}

// Store client options for reference
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
Loading