Skip to content

OpenAICUAClient.ts and AnthropicCUAClient.ts pass the whole clientOptions to the LLM constructor #644

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 4 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/major-goats-follow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": minor
---

Support overriding clientOptions for CUA Client
3 changes: 2 additions & 1 deletion lib/agent/AgentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UnsupportedModelError,
UnsupportedModelProviderError,
} from "@/types/stagehandErrors";
import { ClientOptions } from "@/types/model";

// Map model names to their provider types
const modelToAgentProviderMap: Record<string, AgentType> = {
Expand All @@ -32,7 +33,7 @@ export class AgentProvider {

getClient(
modelName: string,
clientOptions?: Record<string, unknown>,
clientOptions?: ClientOptions & Record<string, unknown>,
userProvidedInstructions?: string,
): AgentClient {
const type = AgentProvider.getAgentProvider(modelName);
Expand Down
14 changes: 5 additions & 9 deletions lib/agent/AnthropicCUAClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Anthropic from "@anthropic-ai/sdk";
import Anthropic, {
type ClientOptions as AnthropicClientOptions,
} from "@anthropic-ai/sdk";
import { LogLine } from "@/types/log";
import {
AgentAction,
Expand Down Expand Up @@ -35,7 +37,7 @@ export class AnthropicCUAClient extends AgentClient {
type: AgentType,
modelName: string,
userProvidedInstructions?: string,
clientOptions?: Record<string, unknown>,
clientOptions?: AnthropicClientOptions & Record<string, unknown>,
) {
super(type, modelName, userProvidedInstructions);

Expand All @@ -53,13 +55,7 @@ export class AnthropicCUAClient extends AgentClient {
}

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

if (this.baseURL) {
this.clientOptions.baseUrl = this.baseURL;
Copy link
Author

@majkiw majkiw Apr 8, 2025

Choose a reason for hiding this comment

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

This probably didn't work as it was setting baseUrl instead of baseURL

}
this.clientOptions = clientOptions;

// Initialize the Anthropic client
this.client = new Anthropic(this.clientOptions);
Expand Down
13 changes: 8 additions & 5 deletions lib/agent/OpenAICUAClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import OpenAI from "openai";
import OpenAI, { type ClientOptions as OpenAIClientOptions } from "openai";
import { LogLine } from "../../types/log";
import {
AgentAction,
Expand Down Expand Up @@ -34,7 +34,7 @@ export class OpenAICUAClient extends AgentClient {
type: AgentType,
modelName: string,
userProvidedInstructions?: string,
clientOptions?: Record<string, unknown>,
clientOptions?: OpenAIClientOptions & Record<string, unknown>,
) {
super(type, modelName, userProvidedInstructions);

Expand All @@ -43,6 +43,7 @@ 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 (
Expand All @@ -53,9 +54,11 @@ export class OpenAICUAClient extends AgentClient {
}

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

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

// Initialize the OpenAI client
this.client = new OpenAI(this.clientOptions);
Expand Down
3 changes: 2 additions & 1 deletion types/agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LogLine } from "./log";
import { ClientOptions } from "@/types/model";

export interface AgentAction {
type: string;
Expand Down Expand Up @@ -44,7 +45,7 @@ export interface AgentExecutionOptions {

export interface AgentHandlerOptions {
modelName: string;
clientOptions?: Record<string, unknown>;
clientOptions?: ClientOptions & Record<string, unknown>;
userProvidedInstructions?: string;
agentType: AgentType;
}
Expand Down
2 changes: 1 addition & 1 deletion types/stagehand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export interface AgentConfig {
/**
* Additional options to pass to the agent client
*/
options?: Record<string, unknown>;
options?: ClientOptions & Record<string, unknown>;
}

export enum StagehandFunctionName {
Expand Down