From 69978a236f2c189223577f43a44560f7071c4920 Mon Sep 17 00:00:00 2001 From: Michal Wysocki Date: Mon, 7 Apr 2025 16:39:25 +0200 Subject: [PATCH 1/4] OpenAICUAClient.ts reads `baseUrl` from `clientOptions` --- lib/agent/OpenAICUAClient.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/agent/OpenAICUAClient.ts b/lib/agent/OpenAICUAClient.ts index 6a494300b..7a48cc75e 100644 --- a/lib/agent/OpenAICUAClient.ts +++ b/lib/agent/OpenAICUAClient.ts @@ -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 ( @@ -57,6 +58,10 @@ export class OpenAICUAClient extends AgentClient { apiKey: this.apiKey, }; + if (this.baseURL) { + this.clientOptions.baseURL = this.baseURL; + } + // Initialize the OpenAI client this.client = new OpenAI(this.clientOptions); } From 52292589d31ab05ea0c4013b4337817ebbd5fa3e Mon Sep 17 00:00:00 2001 From: Michal Wysocki Date: Mon, 7 Apr 2025 16:57:56 +0200 Subject: [PATCH 2/4] CHANGELOG --- .changeset/major-goats-follow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/major-goats-follow.md diff --git a/.changeset/major-goats-follow.md b/.changeset/major-goats-follow.md new file mode 100644 index 000000000..d9a734a97 --- /dev/null +++ b/.changeset/major-goats-follow.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/stagehand": minor +--- + +Support overriding baseURL for OpenAI CUA Agent From df9a79f154b9414854b3571247b6a9a45421e544 Mon Sep 17 00:00:00 2001 From: Michal Wysocki Date: Tue, 8 Apr 2025 10:30:39 +0200 Subject: [PATCH 3/4] Passing the whole ClientOptions into CUA --- lib/agent/AgentProvider.ts | 3 ++- lib/agent/AnthropicCUAClient.ts | 14 +++++--------- lib/agent/OpenAICUAClient.ts | 8 +++----- types/agent.ts | 3 ++- types/stagehand.ts | 2 +- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/agent/AgentProvider.ts b/lib/agent/AgentProvider.ts index cc8110fa4..105d32f83 100644 --- a/lib/agent/AgentProvider.ts +++ b/lib/agent/AgentProvider.ts @@ -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 = { @@ -32,7 +33,7 @@ export class AgentProvider { getClient( modelName: string, - clientOptions?: Record, + clientOptions?: ClientOptions & Record, userProvidedInstructions?: string, ): AgentClient { const type = AgentProvider.getAgentProvider(modelName); diff --git a/lib/agent/AnthropicCUAClient.ts b/lib/agent/AnthropicCUAClient.ts index fbbc97d82..d7027de43 100644 --- a/lib/agent/AnthropicCUAClient.ts +++ b/lib/agent/AnthropicCUAClient.ts @@ -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, @@ -35,7 +37,7 @@ export class AnthropicCUAClient extends AgentClient { type: AgentType, modelName: string, userProvidedInstructions?: string, - clientOptions?: Record, + clientOptions?: AnthropicClientOptions & Record, ) { super(type, modelName, userProvidedInstructions); @@ -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; - } + this.clientOptions = clientOptions; // Initialize the Anthropic client this.client = new Anthropic(this.clientOptions); diff --git a/lib/agent/OpenAICUAClient.ts b/lib/agent/OpenAICUAClient.ts index 7a48cc75e..7e2df4596 100644 --- a/lib/agent/OpenAICUAClient.ts +++ b/lib/agent/OpenAICUAClient.ts @@ -1,4 +1,4 @@ -import OpenAI from "openai"; +import OpenAI, { type ClientOptions as OpenAIClientOptions } from "openai"; import { LogLine } from "../../types/log"; import { AgentAction, @@ -34,7 +34,7 @@ export class OpenAICUAClient extends AgentClient { type: AgentType, modelName: string, userProvidedInstructions?: string, - clientOptions?: Record, + clientOptions?: OpenAIClientOptions & Record, ) { super(type, modelName, userProvidedInstructions); @@ -54,9 +54,7 @@ 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; diff --git a/types/agent.ts b/types/agent.ts index 8cc062012..cd5f830e3 100644 --- a/types/agent.ts +++ b/types/agent.ts @@ -1,4 +1,5 @@ import { LogLine } from "./log"; +import { ClientOptions } from "@/types/model"; export interface AgentAction { type: string; @@ -44,7 +45,7 @@ export interface AgentExecutionOptions { export interface AgentHandlerOptions { modelName: string; - clientOptions?: Record; + clientOptions?: ClientOptions & Record; userProvidedInstructions?: string; agentType: AgentType; } diff --git a/types/stagehand.ts b/types/stagehand.ts index 814ab0886..2233ac41f 100644 --- a/types/stagehand.ts +++ b/types/stagehand.ts @@ -255,7 +255,7 @@ export interface AgentConfig { /** * Additional options to pass to the agent client */ - options?: Record; + options?: ClientOptions & Record; } export enum StagehandFunctionName { From 4fdfea89e5456c24de8627e78649beec1b0ea5cb Mon Sep 17 00:00:00 2001 From: Michal Wysocki Date: Tue, 8 Apr 2025 10:33:11 +0200 Subject: [PATCH 4/4] Fix changeset --- .changeset/major-goats-follow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/major-goats-follow.md b/.changeset/major-goats-follow.md index d9a734a97..d4781ce52 100644 --- a/.changeset/major-goats-follow.md +++ b/.changeset/major-goats-follow.md @@ -2,4 +2,4 @@ "@browserbasehq/stagehand": minor --- -Support overriding baseURL for OpenAI CUA Agent +Support overriding clientOptions for CUA Client