Compare commits
4 Commits
bump-more-
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1db5f1c193 | ||
|
|
ce21468147 | ||
|
|
49575d62f5 | ||
|
|
bf9d44989c |
5
.changeset/opus-4-8-cua.md
Normal file
5
.changeset/opus-4-8-cua.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@browserbasehq/stagehand": patch
|
||||
---
|
||||
|
||||
Add Claude Opus 4.8 to the supported CUA model whitelist.
|
||||
5
.changeset/true-pillows-prove.md
Normal file
5
.changeset/true-pillows-prove.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@browserbasehq/stagehand-server-v3": patch
|
||||
---
|
||||
|
||||
Add SEA binary --version build metadata output.
|
||||
6
.changeset/vertex-model-forwarding.md
Normal file
6
.changeset/vertex-model-forwarding.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"@browserbasehq/stagehand": minor
|
||||
"@browserbasehq/stagehand-server-v3": minor
|
||||
---
|
||||
|
||||
Forward constructor and request model configuration when initializing API-backed sessions.
|
||||
@ -23,6 +23,7 @@ export const modelToAgentProviderMap: Record<string, AgentProviderType> = {
|
||||
"claude-sonnet-4-5-20250929": "anthropic",
|
||||
"claude-opus-4-5-20251101": "anthropic",
|
||||
"claude-opus-4-6": "anthropic",
|
||||
"claude-opus-4-8": "anthropic",
|
||||
"claude-sonnet-4-6": "anthropic",
|
||||
"claude-haiku-4-5": "anthropic",
|
||||
"claude-haiku-4-5-20251001": "anthropic",
|
||||
|
||||
@ -453,6 +453,7 @@ export class AnthropicCUAClient extends AgentClient {
|
||||
|
||||
// Check if this is a Claude 4.6+ model that supports adaptive thinking
|
||||
const isAdaptiveThinkingModel = [
|
||||
"claude-opus-4-8",
|
||||
"claude-opus-4-6",
|
||||
"claude-sonnet-4-6",
|
||||
].includes(modelBase);
|
||||
|
||||
@ -106,6 +106,8 @@ interface ClientSessionStartParams extends Api.SessionStartRequest {
|
||||
* Optional: when omitted, requests are sent without the x-model-api-key header
|
||||
* and the server is expected to handle model authentication on its own. */
|
||||
modelApiKey?: string;
|
||||
/** Default model config for later action requests. Not sent to /sessions/start. */
|
||||
defaultModelConfig?: ModelConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,6 +117,11 @@ type ApiResponse<T> =
|
||||
| { success: true; data: T }
|
||||
| { success: false; message: string };
|
||||
|
||||
type PreparedModelConfig = { modelName: string; apiKey?: string } & Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
|
||||
/**
|
||||
* Union of all API request body types for type-safe execute() calls
|
||||
*/
|
||||
@ -180,6 +187,7 @@ export class StagehandAPIClient {
|
||||
private sessionId?: string;
|
||||
private modelApiKey?: string;
|
||||
private modelProvider?: string;
|
||||
private defaultModelConfig?: PreparedModelConfig;
|
||||
private region?: BrowserbaseRegion;
|
||||
private logger: (message: LogLine) => void;
|
||||
private fetchWithCookies;
|
||||
@ -205,6 +213,7 @@ export class StagehandAPIClient {
|
||||
async init({
|
||||
modelName,
|
||||
modelApiKey,
|
||||
defaultModelConfig,
|
||||
domSettleTimeoutMs,
|
||||
verbose,
|
||||
systemPrompt,
|
||||
@ -218,6 +227,9 @@ export class StagehandAPIClient {
|
||||
this.modelProvider = modelName?.includes("/")
|
||||
? modelName.split("/")[0]
|
||||
: undefined;
|
||||
this.defaultModelConfig = defaultModelConfig
|
||||
? this.prepareModelConfig(defaultModelConfig)
|
||||
: undefined;
|
||||
|
||||
// Store the region for multi-region API URL resolution
|
||||
this.region = browserbaseSessionCreateParams?.region;
|
||||
@ -288,12 +300,18 @@ export class StagehandAPIClient {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { page: _, serverCache: enableCache, ...restOptions } = options;
|
||||
serverCache = enableCache;
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
} else if (this.defaultModelConfig) {
|
||||
restOptions.model = this.getDefaultModelConfig();
|
||||
}
|
||||
if (Object.keys(restOptions).length > 0) {
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
}
|
||||
wireOptions = restOptions as unknown as Api.ActRequest["options"];
|
||||
}
|
||||
} else if (this.defaultModelConfig) {
|
||||
wireOptions = {
|
||||
model: this.getDefaultModelConfig(),
|
||||
} as unknown as Api.ActRequest["options"];
|
||||
}
|
||||
|
||||
// Build wire-format request body
|
||||
@ -326,12 +344,18 @@ export class StagehandAPIClient {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { page: _, serverCache: enableCache, ...restOptions } = options;
|
||||
serverCache = enableCache;
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
} else if (this.defaultModelConfig) {
|
||||
restOptions.model = this.getDefaultModelConfig();
|
||||
}
|
||||
if (Object.keys(restOptions).length > 0) {
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
}
|
||||
wireOptions = restOptions as unknown as Api.ExtractRequest["options"];
|
||||
}
|
||||
} else if (this.defaultModelConfig) {
|
||||
wireOptions = {
|
||||
model: this.getDefaultModelConfig(),
|
||||
} as unknown as Api.ExtractRequest["options"];
|
||||
}
|
||||
|
||||
// Build wire-format request body
|
||||
@ -361,12 +385,18 @@ export class StagehandAPIClient {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { page: _, serverCache: enableCache, ...restOptions } = options;
|
||||
serverCache = enableCache;
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
} else if (this.defaultModelConfig) {
|
||||
restOptions.model = this.getDefaultModelConfig();
|
||||
}
|
||||
if (Object.keys(restOptions).length > 0) {
|
||||
if (restOptions.model) {
|
||||
restOptions.model = this.prepareModelConfig(restOptions.model);
|
||||
}
|
||||
wireOptions = restOptions as unknown as Api.ObserveRequest["options"];
|
||||
}
|
||||
} else if (this.defaultModelConfig) {
|
||||
wireOptions = {
|
||||
model: this.getDefaultModelConfig(),
|
||||
} as unknown as Api.ObserveRequest["options"];
|
||||
}
|
||||
|
||||
// Build wire-format request body
|
||||
@ -388,7 +418,22 @@ export class StagehandAPIClient {
|
||||
options?: Api.NavigateRequest["options"],
|
||||
frameId?: string,
|
||||
): Promise<SerializableResponse | null> {
|
||||
const requestBody: Api.NavigateRequest = { url, options, frameId };
|
||||
const publicOptions = { ...(options ?? {}) } as NonNullable<
|
||||
Api.NavigateRequest["options"]
|
||||
> & { model?: unknown };
|
||||
delete publicOptions.model;
|
||||
|
||||
const wireOptions = {
|
||||
...publicOptions,
|
||||
...(this.defaultModelConfig
|
||||
? { model: this.getDefaultModelConfig() }
|
||||
: {}),
|
||||
};
|
||||
const requestBody: Api.NavigateRequest = {
|
||||
url,
|
||||
options: Object.keys(wireOptions).length > 0 ? wireOptions : undefined,
|
||||
frameId,
|
||||
};
|
||||
|
||||
return this.execute<SerializableResponse | null>({
|
||||
method: "navigate",
|
||||
@ -425,7 +470,7 @@ export class StagehandAPIClient {
|
||||
cua: agentConfig.mode === undefined ? agentConfig.cua : undefined,
|
||||
model: agentConfig.model
|
||||
? this.prepareModelConfig(agentConfig.model)
|
||||
: undefined,
|
||||
: this.getDefaultModelConfig(),
|
||||
executionModel: agentConfig.executionModel
|
||||
? this.prepareModelConfig(agentConfig.executionModel)
|
||||
: undefined,
|
||||
@ -605,40 +650,52 @@ export class StagehandAPIClient {
|
||||
* In API mode, we only attempt to load an API key from env vars when the
|
||||
* model provider differs from the one used to init the session.
|
||||
*/
|
||||
private prepareModelConfig(
|
||||
model: ModelConfiguration,
|
||||
): { modelName: string; apiKey?: string } & Record<string, unknown> {
|
||||
private prepareModelConfig(model: ModelConfiguration): PreparedModelConfig {
|
||||
if (typeof model === "string") {
|
||||
// Extract provider from model string (e.g., "openai/gpt-5-nano" -> "openai")
|
||||
const provider = model.includes("/") ? model.split("/")[0] : undefined;
|
||||
const provider = this.getModelProvider(model);
|
||||
const inheritedDefault =
|
||||
provider && provider === this.modelProvider
|
||||
? this.getDefaultModelConfig()
|
||||
: undefined;
|
||||
const apiKey =
|
||||
provider && provider !== this.modelProvider
|
||||
? (loadApiKeyFromEnv(provider, this.logger) ?? this.modelApiKey)
|
||||
: this.modelApiKey;
|
||||
return {
|
||||
...inheritedDefault,
|
||||
modelName: model,
|
||||
...(apiKey ? { apiKey } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
if (!model.apiKey) {
|
||||
const provider = model.modelName?.includes("/")
|
||||
? model.modelName.split("/")[0]
|
||||
const provider = this.getModelProvider(model.modelName);
|
||||
const inheritedDefault =
|
||||
provider && provider === this.modelProvider
|
||||
? this.getDefaultModelConfig()
|
||||
: undefined;
|
||||
const apiKey =
|
||||
provider && provider !== this.modelProvider
|
||||
? (loadApiKeyFromEnv(provider, this.logger) ?? this.modelApiKey)
|
||||
: this.modelApiKey;
|
||||
return {
|
||||
...model,
|
||||
...(apiKey ? { apiKey } : {}),
|
||||
};
|
||||
}
|
||||
const apiKey =
|
||||
!model.apiKey && provider && provider !== this.modelProvider
|
||||
? (loadApiKeyFromEnv(provider, this.logger) ?? this.modelApiKey)
|
||||
: !model.apiKey
|
||||
? this.modelApiKey
|
||||
: undefined;
|
||||
|
||||
return model as { modelName: string; apiKey: string } & Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
return {
|
||||
...inheritedDefault,
|
||||
...model,
|
||||
...(apiKey ? { apiKey } : {}),
|
||||
} as PreparedModelConfig;
|
||||
}
|
||||
|
||||
private getDefaultModelConfig(): PreparedModelConfig | undefined {
|
||||
return this.defaultModelConfig
|
||||
? ({ ...this.defaultModelConfig } as PreparedModelConfig)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
private getModelProvider(modelName: string | undefined): string | undefined {
|
||||
return modelName?.includes("/") ? modelName.split("/")[0] : undefined;
|
||||
}
|
||||
|
||||
private consumeFinishedEventData<T>(): T | null {
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import type { LanguageModelV2Middleware } from "@ai-sdk/provider";
|
||||
import {
|
||||
ExperimentalNotConfiguredError,
|
||||
UnsupportedAISDKModelProviderError,
|
||||
UnsupportedModelError,
|
||||
UnsupportedModelProviderError,
|
||||
@ -70,6 +69,37 @@ const AISDKProvidersWithAPIKey: Record<string, AISDKCustomProvider> = {
|
||||
gateway: createGateway,
|
||||
};
|
||||
|
||||
type AISDKProviderClientOptions = ClientOptions & Record<string, unknown>;
|
||||
|
||||
export function toAISDKClientOptions(
|
||||
subProvider: string,
|
||||
clientOptions?: ClientOptions,
|
||||
): AISDKProviderClientOptions | undefined {
|
||||
if (!clientOptions || subProvider !== "vertex") {
|
||||
return clientOptions as AISDKProviderClientOptions | undefined;
|
||||
}
|
||||
|
||||
const { auth, providerOptions, ...rest } = clientOptions;
|
||||
const vertexOptions = providerOptions?.vertex;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
...(vertexOptions ?? {}),
|
||||
...(auth?.type === "googleServiceAccount"
|
||||
? {
|
||||
googleAuthOptions: {
|
||||
credentials: auth.credentials,
|
||||
...(auth.scopes ? { scopes: auth.scopes } : {}),
|
||||
...(auth.projectId ? { projectId: auth.projectId } : {}),
|
||||
...(auth.universeDomain
|
||||
? { universeDomain: auth.universeDomain }
|
||||
: {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
const modelToProviderMap: { [key in AvailableModel]: ModelProvider } = {
|
||||
"gpt-4.1": "openai",
|
||||
"gpt-4.1-mini": "openai",
|
||||
@ -106,9 +136,12 @@ export function getAISDKLanguageModel(
|
||||
clientOptions?: ClientOptions,
|
||||
middleware?: LanguageModelV2Middleware,
|
||||
) {
|
||||
const aiSdkClientOptions = toAISDKClientOptions(subProvider, clientOptions);
|
||||
const hasValidOptions =
|
||||
clientOptions &&
|
||||
Object.values(clientOptions).some((v) => v !== undefined && v !== null);
|
||||
aiSdkClientOptions &&
|
||||
Object.values(aiSdkClientOptions).some(
|
||||
(v) => v !== undefined && v !== null,
|
||||
);
|
||||
|
||||
let model;
|
||||
if (hasValidOptions) {
|
||||
@ -119,7 +152,7 @@ export function getAISDKLanguageModel(
|
||||
Object.keys(AISDKProvidersWithAPIKey),
|
||||
);
|
||||
}
|
||||
const provider = creator(clientOptions);
|
||||
const provider = creator(aiSdkClientOptions as ClientOptions);
|
||||
model = provider(subModelName);
|
||||
} else {
|
||||
const provider = AISDKProviders[subProvider];
|
||||
@ -163,13 +196,6 @@ export class LLMProvider {
|
||||
const firstSlashIndex = modelName.indexOf("/");
|
||||
const subProvider = modelName.substring(0, firstSlashIndex);
|
||||
const subModelName = modelName.substring(firstSlashIndex + 1);
|
||||
if (
|
||||
subProvider === "vertex" &&
|
||||
!options?.disableAPI &&
|
||||
!options?.experimental
|
||||
) {
|
||||
throw new ExperimentalNotConfiguredError("Vertex provider");
|
||||
}
|
||||
|
||||
const effectiveMiddleware = options?.middleware ?? this.middleware;
|
||||
const languageModel = getAISDKLanguageModel(
|
||||
|
||||
@ -462,6 +462,7 @@ export const AVAILABLE_CUA_MODELS = [
|
||||
"openai/computer-use-preview-2025-03-11",
|
||||
"anthropic/claude-opus-4-5-20251101",
|
||||
"anthropic/claude-opus-4-6",
|
||||
"anthropic/claude-opus-4-8",
|
||||
"anthropic/claude-sonnet-4-6",
|
||||
"anthropic/claude-haiku-4-5",
|
||||
"anthropic/claude-haiku-4-5-20251001",
|
||||
|
||||
@ -73,9 +73,13 @@ export const GoogleServiceAccountCredentialsSchema = z
|
||||
.strict()
|
||||
.meta({ id: "GoogleServiceAccountCredentials" });
|
||||
|
||||
export const GoogleAuthOptionsSchema = z
|
||||
export const GoogleServiceAccountAuthSchema = z
|
||||
.object({
|
||||
credentials: GoogleServiceAccountCredentialsSchema.optional().meta({
|
||||
type: z.literal("googleServiceAccount").meta({
|
||||
description:
|
||||
"Use inline Google Cloud service account credentials for provider authentication",
|
||||
}),
|
||||
credentials: GoogleServiceAccountCredentialsSchema.meta({
|
||||
description: "Google Cloud service account credentials",
|
||||
}),
|
||||
scopes: z
|
||||
@ -92,18 +96,44 @@ export const GoogleAuthOptionsSchema = z
|
||||
}),
|
||||
})
|
||||
.strict()
|
||||
.meta({ id: "GoogleAuthOptions" });
|
||||
.meta({ id: "GoogleServiceAccountAuth" });
|
||||
|
||||
export const ModelConfigObjectSchema = z
|
||||
export const ModelAuthSchema = z
|
||||
.discriminatedUnion("type", [GoogleServiceAccountAuthSchema])
|
||||
.meta({ id: "ModelAuth" });
|
||||
|
||||
export const VertexProviderOptionsSchema = z
|
||||
.object({
|
||||
project: z.string().meta({
|
||||
description: "Google Cloud project ID for Vertex AI models",
|
||||
example: "my-gcp-project",
|
||||
}),
|
||||
location: z.string().meta({
|
||||
description: "Google Cloud location for Vertex AI models",
|
||||
example: "us-central1",
|
||||
}),
|
||||
baseURL: z.string().url().optional().meta({
|
||||
description: "Base URL for the Vertex AI provider",
|
||||
}),
|
||||
headers: z.record(z.string(), z.string()).optional().meta({
|
||||
description:
|
||||
"Custom headers sent with every request to the Vertex AI provider",
|
||||
}),
|
||||
})
|
||||
.strict()
|
||||
.meta({ id: "VertexProviderOptions" });
|
||||
|
||||
export const ModelProviderOptionsSchema = z
|
||||
.object({
|
||||
vertex: VertexProviderOptionsSchema.meta({
|
||||
description: "Vertex AI provider-specific settings",
|
||||
}),
|
||||
})
|
||||
.strict()
|
||||
.meta({ id: "ModelProviderOptions" });
|
||||
|
||||
const ModelConfigBaseSchema = z
|
||||
.object({
|
||||
provider: z
|
||||
.enum(["openai", "anthropic", "google", "microsoft", "bedrock", "vertex"])
|
||||
.optional()
|
||||
.meta({
|
||||
description:
|
||||
"AI provider for the model (or provide a baseURL endpoint instead)",
|
||||
example: "openai",
|
||||
}),
|
||||
modelName: z.string().meta({
|
||||
description:
|
||||
"Model name string with provider prefix (e.g., 'openai/gpt-5-nano')",
|
||||
@ -121,19 +151,38 @@ export const ModelConfigObjectSchema = z
|
||||
description:
|
||||
"Custom headers sent with every request to the model provider",
|
||||
}),
|
||||
project: z.string().optional().meta({
|
||||
description: "Google Cloud project ID for Vertex AI models",
|
||||
example: "my-gcp-project",
|
||||
}),
|
||||
location: z.string().optional().meta({
|
||||
description: "Google Cloud location for Vertex AI models",
|
||||
example: "us-central1",
|
||||
}),
|
||||
googleAuthOptions: GoogleAuthOptionsSchema.optional().meta({
|
||||
description:
|
||||
"google-auth-library options used to authenticate Vertex AI models",
|
||||
}),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export const GenericModelConfigObjectSchema = ModelConfigBaseSchema.extend({
|
||||
provider: z
|
||||
.enum(["openai", "anthropic", "google", "microsoft", "bedrock"])
|
||||
.optional()
|
||||
.meta({
|
||||
description:
|
||||
"AI provider for the model (or provide a baseURL endpoint instead)",
|
||||
example: "openai",
|
||||
}),
|
||||
})
|
||||
.strict()
|
||||
.meta({ id: "GenericModelConfigObject" });
|
||||
|
||||
export const VertexModelConfigObjectSchema = ModelConfigBaseSchema.extend({
|
||||
provider: z.literal("vertex").meta({
|
||||
description: "Vertex AI model provider",
|
||||
}),
|
||||
auth: ModelAuthSchema.meta({
|
||||
description: "Vertex provider authentication configuration",
|
||||
}),
|
||||
providerOptions: ModelProviderOptionsSchema.meta({
|
||||
description: "Vertex provider-specific model configuration",
|
||||
}),
|
||||
})
|
||||
.strict()
|
||||
.meta({ id: "VertexModelConfigObject" });
|
||||
|
||||
export const ModelConfigObjectSchema = z
|
||||
.union([VertexModelConfigObjectSchema, GenericModelConfigObjectSchema])
|
||||
.meta({ id: "ModelConfigObject" });
|
||||
|
||||
/** Model configuration */
|
||||
@ -1161,6 +1210,21 @@ export const Operations = {
|
||||
// Shared types
|
||||
export type Action = z.infer<typeof ActionSchema>;
|
||||
export type ModelConfig = z.infer<typeof ModelConfigSchema>;
|
||||
export type GenericModelConfigObject = z.infer<
|
||||
typeof GenericModelConfigObjectSchema
|
||||
>;
|
||||
export type VertexModelConfigObject = z.infer<
|
||||
typeof VertexModelConfigObjectSchema
|
||||
>;
|
||||
export type GoogleServiceAccountCredentials = z.infer<
|
||||
typeof GoogleServiceAccountCredentialsSchema
|
||||
>;
|
||||
export type GoogleServiceAccountAuth = z.infer<
|
||||
typeof GoogleServiceAccountAuthSchema
|
||||
>;
|
||||
export type ModelAuth = z.infer<typeof ModelAuthSchema>;
|
||||
export type VertexProviderOptions = z.infer<typeof VertexProviderOptionsSchema>;
|
||||
export type ModelProviderOptions = z.infer<typeof ModelProviderOptionsSchema>;
|
||||
export type BrowserConfig = z.infer<typeof BrowserConfigSchema>;
|
||||
export type SessionIdParams = z.infer<typeof SessionIdParamsSchema>;
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import type { ClientOptions as AnthropicClientOptionsBase } from "@anthropic-ai/sdk";
|
||||
import type { GoogleVertexProviderSettings as GoogleVertexProviderSettingsBase } from "@ai-sdk/google-vertex";
|
||||
import type {
|
||||
LanguageModelV2,
|
||||
LanguageModelV2Middleware,
|
||||
@ -31,19 +30,26 @@ export interface GoogleServiceAccountCredentials {
|
||||
universe_domain?: string;
|
||||
}
|
||||
|
||||
export interface GoogleVertexAuthOptions {
|
||||
credentials?: GoogleServiceAccountCredentials;
|
||||
export interface GoogleServiceAccountAuth {
|
||||
type: "googleServiceAccount";
|
||||
credentials: GoogleServiceAccountCredentials;
|
||||
scopes?: string | string[];
|
||||
projectId?: string;
|
||||
universeDomain?: string;
|
||||
}
|
||||
|
||||
export type GoogleVertexProviderSettings = Pick<
|
||||
GoogleVertexProviderSettingsBase,
|
||||
"project" | "location" | "headers" | "baseURL"
|
||||
> & {
|
||||
googleAuthOptions?: GoogleVertexAuthOptions;
|
||||
};
|
||||
export type ModelAuth = GoogleServiceAccountAuth;
|
||||
|
||||
export interface VertexProviderOptions {
|
||||
project: string;
|
||||
location: string;
|
||||
baseURL?: string;
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface ModelProviderOptions {
|
||||
vertex: VertexProviderOptions;
|
||||
}
|
||||
|
||||
export type AnthropicJsonSchemaObject = {
|
||||
definitions?: {
|
||||
@ -113,13 +119,11 @@ export type ModelProvider =
|
||||
*/
|
||||
export type ThinkingEffort = "none" | "low" | "medium" | "high" | "max";
|
||||
|
||||
export type ClientOptions = (
|
||||
| OpenAIClientOptions
|
||||
| AnthropicClientOptions
|
||||
| GoogleVertexProviderSettings
|
||||
) & {
|
||||
export type ClientOptions = (OpenAIClientOptions | AnthropicClientOptions) & {
|
||||
apiKey?: string;
|
||||
provider?: AgentProviderType;
|
||||
auth?: ModelAuth;
|
||||
providerOptions?: ModelProviderOptions;
|
||||
baseURL?: string;
|
||||
/** OpenAI organization ID */
|
||||
organization?: string;
|
||||
|
||||
@ -538,6 +538,26 @@ export class V3 {
|
||||
);
|
||||
}
|
||||
|
||||
private getApiDefaultModelConfig(): ModelConfiguration | undefined {
|
||||
const clientOptions = Object.fromEntries(
|
||||
Object.entries(this.modelClientOptions).filter(
|
||||
([key, value]) => key !== "apiKey" && value !== undefined,
|
||||
),
|
||||
);
|
||||
|
||||
if (Object.keys(clientOptions).length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
modelName: this.modelName,
|
||||
...clientOptions,
|
||||
...((this.modelClientOptions as { apiKey?: string }).apiKey
|
||||
? { apiKey: (this.modelClientOptions as { apiKey?: string }).apiKey }
|
||||
: {}),
|
||||
} as ModelConfiguration;
|
||||
}
|
||||
|
||||
private resolveLlmClient(model?: ModelConfiguration): LLMClient {
|
||||
if (!model) {
|
||||
return this.llmClient;
|
||||
@ -1043,6 +1063,7 @@ export class V3 {
|
||||
const { sessionId, available } = await this.apiClient.init({
|
||||
modelName: this.modelName,
|
||||
modelApiKey: this.modelClientOptions.apiKey,
|
||||
defaultModelConfig: this.getApiDefaultModelConfig(),
|
||||
domSettleTimeoutMs: this.domSettleTimeoutMs,
|
||||
verbose: this.verbose,
|
||||
systemPrompt: this.opts.systemPrompt,
|
||||
|
||||
@ -36,7 +36,30 @@ describe("AnthropicCUAClient adaptive thinking", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Claude 4.6 models (adaptive thinking)", () => {
|
||||
describe("Claude 4.6+ models (adaptive thinking)", () => {
|
||||
it("should use thinking.type: 'adaptive' for claude-opus-4-8 when thinkingEffort is set", async () => {
|
||||
const client = new AnthropicCUAClient(
|
||||
"anthropic",
|
||||
"claude-opus-4-8",
|
||||
undefined,
|
||||
{
|
||||
apiKey: "test-key",
|
||||
thinkingEffort: "high",
|
||||
},
|
||||
);
|
||||
client.setViewport(1280, 720);
|
||||
|
||||
await client.getAction([{ role: "user", content: "test" }]);
|
||||
|
||||
expect(mockCreate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
thinking: { type: "adaptive" },
|
||||
output_config: { effort: "high" },
|
||||
temperature: 1,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("should use thinking.type: 'adaptive' for claude-opus-4-6 when thinkingEffort is set", async () => {
|
||||
const client = new AnthropicCUAClient(
|
||||
"anthropic",
|
||||
|
||||
342
packages/core/tests/unit/api-client-default-model-config.test.ts
Normal file
342
packages/core/tests/unit/api-client-default-model-config.test.ts
Normal file
@ -0,0 +1,342 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { StagehandAPIClient } from "../../lib/v3/api.js";
|
||||
import type { ModelConfiguration } from "../../lib/v3/types/public/model.js";
|
||||
|
||||
const vertexModel = {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
} as unknown as ModelConfiguration;
|
||||
|
||||
function createClientWithExecuteMock() {
|
||||
const client = new StagehandAPIClient({
|
||||
apiKey: "bb-test",
|
||||
logger: vi.fn(),
|
||||
});
|
||||
const executeMock = vi.fn().mockResolvedValue({});
|
||||
|
||||
(
|
||||
client as unknown as {
|
||||
execute: typeof executeMock;
|
||||
}
|
||||
).execute = executeMock;
|
||||
|
||||
return { client, executeMock };
|
||||
}
|
||||
|
||||
describe("StagehandAPIClient default model config", () => {
|
||||
let originalFetch: typeof globalThis.fetch;
|
||||
|
||||
beforeEach(() => {
|
||||
originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = vi.fn().mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
data: { sessionId: "sess-default-model", available: true },
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("sends constructor Vertex model config on navigate bootstrap, act, observe, extract, and agent execute requests", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
defaultModelConfig: vertexModel,
|
||||
});
|
||||
|
||||
await client.goto("https://example.com");
|
||||
await client.act({ input: "click the login button" });
|
||||
await client.observe({ instruction: "find the login button" });
|
||||
await client.extract({ instruction: "extract the headline" });
|
||||
await client.agentExecute({ mode: "dom" }, "click the login button");
|
||||
|
||||
expect(executeMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
expect.objectContaining({
|
||||
method: "navigate",
|
||||
args: expect.objectContaining({
|
||||
options: {
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(executeMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
expect.objectContaining({
|
||||
method: "act",
|
||||
args: expect.objectContaining({
|
||||
options: {
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(executeMock).toHaveBeenNthCalledWith(
|
||||
3,
|
||||
expect.objectContaining({
|
||||
method: "observe",
|
||||
args: expect.objectContaining({
|
||||
options: expect.objectContaining({
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(executeMock).toHaveBeenNthCalledWith(
|
||||
4,
|
||||
expect.objectContaining({
|
||||
method: "extract",
|
||||
args: expect.objectContaining({
|
||||
options: expect.objectContaining({
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(executeMock).toHaveBeenNthCalledWith(
|
||||
5,
|
||||
expect.objectContaining({
|
||||
method: "agentExecute",
|
||||
args: expect.objectContaining({
|
||||
agentConfig: expect.objectContaining({
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("prefers a per-call model over the constructor default model config", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
defaultModelConfig: vertexModel,
|
||||
});
|
||||
|
||||
await client.act({
|
||||
input: "click the login button",
|
||||
options: {
|
||||
model: {
|
||||
modelName: "openai/gpt-4.1-mini",
|
||||
apiKey: "sk-per-call",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(executeMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({
|
||||
options: expect.objectContaining({
|
||||
model: {
|
||||
modelName: "openai/gpt-4.1-mini",
|
||||
apiKey: "sk-per-call",
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("inherits constructor Vertex auth for a same-provider string model override", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
defaultModelConfig: vertexModel,
|
||||
});
|
||||
|
||||
await client.observe({
|
||||
instruction: "find the login button",
|
||||
options: {
|
||||
model: "vertex/gemini-2.0-flash",
|
||||
},
|
||||
});
|
||||
|
||||
expect(executeMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: "observe",
|
||||
args: expect.objectContaining({
|
||||
options: expect.objectContaining({
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.0-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not treat model as a public navigate option", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "openai/gpt-4.1-mini",
|
||||
modelApiKey: "sk-header-only",
|
||||
});
|
||||
|
||||
await client.goto("https://example.com", {
|
||||
model: {
|
||||
modelName: "openai/gpt-4o",
|
||||
apiKey: "sk-per-call",
|
||||
},
|
||||
} as unknown as Parameters<StagehandAPIClient["goto"]>[1]);
|
||||
|
||||
expect(executeMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: "navigate",
|
||||
args: expect.objectContaining({
|
||||
options: undefined,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps constructor bootstrap config when a caller passes an unsupported navigate model", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
defaultModelConfig: vertexModel,
|
||||
});
|
||||
|
||||
await client.goto("https://example.com", {
|
||||
model: {
|
||||
modelName: "openai/gpt-4o",
|
||||
apiKey: "sk-per-call",
|
||||
},
|
||||
} as unknown as Parameters<StagehandAPIClient["goto"]>[1]);
|
||||
|
||||
expect(executeMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: "navigate",
|
||||
args: expect.objectContaining({
|
||||
options: {
|
||||
model: expect.objectContaining({
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: expect.objectContaining({
|
||||
type: "googleServiceAccount",
|
||||
}),
|
||||
providerOptions: {
|
||||
vertex: expect.objectContaining({
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
}),
|
||||
},
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not add a body model config when the constructor only provides a model API key", async () => {
|
||||
const { client, executeMock } = createClientWithExecuteMock();
|
||||
|
||||
await client.init({
|
||||
modelName: "openai/gpt-4.1-mini",
|
||||
modelApiKey: "sk-header-only",
|
||||
});
|
||||
|
||||
await client.act({ input: "click the login button" });
|
||||
|
||||
expect(executeMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
args: expect.objectContaining({
|
||||
options: undefined,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -73,9 +73,8 @@ describe("API model config schemas", () => {
|
||||
const vertexModel = {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
googleAuthOptions: {
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
type: "service_account",
|
||||
project_id: "test-gcp-project",
|
||||
@ -89,6 +88,12 @@ describe("API model config schemas", () => {
|
||||
projectId: "test-gcp-project",
|
||||
universeDomain: "googleapis.com",
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
it("preserves Vertex auth params for act requests", () => {
|
||||
@ -111,15 +116,20 @@ describe("API model config schemas", () => {
|
||||
model: {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
googleAuthOptions: {
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -131,7 +141,11 @@ describe("API model config schemas", () => {
|
||||
if (typeof parsedModel !== "object" || parsedModel === null) {
|
||||
throw new Error("Expected object model config");
|
||||
}
|
||||
expect(parsedModel.googleAuthOptions).toEqual({
|
||||
if (!("auth" in parsedModel)) {
|
||||
throw new Error("Expected Vertex auth config");
|
||||
}
|
||||
expect(parsedModel.auth).toEqual({
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
@ -140,6 +154,67 @@ describe("API model config schemas", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("requires explicit Vertex provider configs to include service account auth and provider options", () => {
|
||||
for (const model of [
|
||||
{
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
},
|
||||
]) {
|
||||
const result = Api.ActRequestSchema.safeParse({
|
||||
input: "click the search button",
|
||||
options: { model },
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it("requires provider vertex when passing Vertex auth and provider options", () => {
|
||||
const result = Api.ActRequestSchema.safeParse({
|
||||
input: "click the search button",
|
||||
options: {
|
||||
model: {
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves Vertex auth params for agent model configs", () => {
|
||||
const result = Api.AgentExecuteRequestSchema.safeParse({
|
||||
agentConfig: {
|
||||
@ -158,6 +233,20 @@ describe("API model config schemas", () => {
|
||||
});
|
||||
|
||||
it("rejects Vertex key file paths and external credential sources", () => {
|
||||
const topLevelLegacyShape = Api.ActRequestSchema.safeParse({
|
||||
input: "click the search button",
|
||||
options: {
|
||||
model: {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
googleAuthOptions: {},
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(topLevelLegacyShape.success).toBe(false);
|
||||
|
||||
for (const blockedAuthOptions of [
|
||||
{ keyFilename: "/etc/passwd" },
|
||||
{ keyFile: "/etc/passwd" },
|
||||
@ -169,7 +258,7 @@ describe("API model config schemas", () => {
|
||||
model: {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
googleAuthOptions: blockedAuthOptions,
|
||||
auth: blockedAuthOptions,
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -183,7 +272,8 @@ describe("API model config schemas", () => {
|
||||
model: {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
googleAuthOptions: {
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
type: "external_account",
|
||||
credential_source: {
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getAISDKLanguageModel } from "../../lib/v3/llm/LLMProvider.js";
|
||||
import {
|
||||
getAISDKLanguageModel,
|
||||
LLMProvider,
|
||||
toAISDKClientOptions,
|
||||
} from "../../lib/v3/llm/LLMProvider.js";
|
||||
|
||||
describe("getAISDKLanguageModel", () => {
|
||||
describe("ollama provider", () => {
|
||||
@ -68,3 +72,67 @@ describe("getAISDKLanguageModel", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("LLMProvider", () => {
|
||||
it("allows Vertex models without experimental mode", () => {
|
||||
const provider = new LLMProvider(() => {});
|
||||
|
||||
expect(() =>
|
||||
provider.getClient(
|
||||
"vertex/gemini-2.5-flash" as never,
|
||||
{
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "stagehand@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
} as never,
|
||||
{ experimental: false, disableAPI: false },
|
||||
),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("adapts canonical Vertex auth into AI SDK googleAuthOptions", () => {
|
||||
expect(
|
||||
toAISDKClientOptions("vertex", {
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "stagehand@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n",
|
||||
},
|
||||
projectId: "test-project",
|
||||
universeDomain: "googleapis.com",
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
project: "test-project",
|
||||
location: "us-central1",
|
||||
googleAuthOptions: {
|
||||
credentials: {
|
||||
client_email: "stagehand@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n",
|
||||
},
|
||||
projectId: "test-project",
|
||||
universeDomain: "googleapis.com",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -3,17 +3,30 @@ import * as Stagehand from "@browserbasehq/stagehand";
|
||||
|
||||
describe("LLM and Agents public API types", () => {
|
||||
describe("ModelConfiguration", () => {
|
||||
it("accepts Vertex headers in model config", () => {
|
||||
const googleConfig = {
|
||||
modelName: "google/gemini-3-flash-preview",
|
||||
project: "test-project",
|
||||
location: "global",
|
||||
it("accepts Vertex provider options in model config", () => {
|
||||
const vertexConfig = {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-3-flash-preview",
|
||||
headers: {
|
||||
"X-Goog-Priority": "high",
|
||||
},
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-project",
|
||||
location: "global",
|
||||
},
|
||||
},
|
||||
} satisfies Stagehand.ModelConfiguration;
|
||||
|
||||
void googleConfig;
|
||||
void vertexConfig;
|
||||
});
|
||||
});
|
||||
|
||||
@ -44,6 +57,7 @@ describe("LLM and Agents public API types", () => {
|
||||
"openai/gpt-5.5",
|
||||
"anthropic/claude-opus-4-5-20251101",
|
||||
"anthropic/claude-opus-4-6",
|
||||
"anthropic/claude-opus-4-8",
|
||||
"anthropic/claude-sonnet-4-6",
|
||||
"anthropic/claude-haiku-4-5",
|
||||
"anthropic/claude-haiku-4-5-20251001",
|
||||
@ -61,6 +75,12 @@ describe("LLM and Agents public API types", () => {
|
||||
>();
|
||||
void expectedModels; // Mark as used to satisfy ESLint
|
||||
});
|
||||
|
||||
it("includes Claude Opus 4.8 at runtime", () => {
|
||||
expect(Stagehand.AVAILABLE_CUA_MODELS).toContain(
|
||||
"anthropic/claude-opus-4-8",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("AgentProvider", () => {
|
||||
@ -274,6 +294,12 @@ describe("LLM and Agents public API types", () => {
|
||||
typeof Stagehand.modelToAgentProviderMap
|
||||
>().toExtend<ExpectedModelToAgentProviderMap>();
|
||||
});
|
||||
|
||||
it("routes Claude Opus 4.8 to Anthropic", () => {
|
||||
expect(Stagehand.modelToAgentProviderMap["claude-opus-4-8"]).toBe(
|
||||
"anthropic",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Response", () => {
|
||||
|
||||
@ -669,6 +669,7 @@ const agent = stagehand.agent({
|
||||
| Anthropic | `anthropic/claude-sonnet-4-5-20250929` |
|
||||
| Anthropic | `anthropic/claude-opus-4-5-20251101` |
|
||||
| Anthropic | `anthropic/claude-opus-4-6` |
|
||||
| Anthropic | `anthropic/claude-opus-4-8` |
|
||||
| Google | `google/gemini-2.5-computer-use-preview-10-2025` |
|
||||
| Google | `google/gemini-3-flash-preview` |
|
||||
| Google | `google/gemini-3-pro-preview` |
|
||||
|
||||
@ -72,6 +72,7 @@ interface AgentInstance {
|
||||
- `"anthropic/claude-sonnet-4-5-20250929"`
|
||||
- `"anthropic/claude-opus-4-5-20251101"`
|
||||
- `"anthropic/claude-opus-4-6"`
|
||||
- `"anthropic/claude-opus-4-8"`
|
||||
- `"google/gemini-2.5-computer-use-preview-10-2025"`
|
||||
- `"google/gemini-3-flash-preview"`
|
||||
- `"google/gemini-3-pro-preview"`
|
||||
@ -867,4 +868,4 @@ The following errors may be thrown by the `agent()` method:
|
||||
- **StagehandDefaultError** - General execution error with detailed message
|
||||
- **AgentAbortError** - Thrown when agent execution is cancelled via an `AbortSignal`
|
||||
- **StreamingCallbacksInNonStreamingModeError** - Thrown when streaming-only callbacks (`onChunk`, `onFinish`, `onError`, `onAbort`) are used without `stream: true`
|
||||
- **ExperimentalNotConfiguredError** - Thrown when experimental features (callbacks, signal, messages, streaming) are used without `experimental: true` in Stagehand constructor
|
||||
- **ExperimentalNotConfiguredError** - Thrown when experimental features (callbacks, signal, messages, streaming) are used without `experimental: true` in Stagehand constructor
|
||||
|
||||
@ -202,9 +202,14 @@ components:
|
||||
- private_key
|
||||
- client_email
|
||||
additionalProperties: false
|
||||
GoogleAuthOptions:
|
||||
GoogleServiceAccountAuth:
|
||||
type: object
|
||||
properties:
|
||||
type:
|
||||
description: Use inline Google Cloud service account credentials for provider
|
||||
authentication
|
||||
type: string
|
||||
const: googleServiceAccount
|
||||
credentials:
|
||||
description: Google Cloud service account credentials
|
||||
$ref: "#/components/schemas/GoogleServiceAccountCredentials"
|
||||
@ -221,21 +226,51 @@ components:
|
||||
universeDomain:
|
||||
description: Google Cloud universe domain
|
||||
type: string
|
||||
required:
|
||||
- type
|
||||
- credentials
|
||||
additionalProperties: false
|
||||
ModelConfigObject:
|
||||
ModelAuth:
|
||||
oneOf:
|
||||
- $ref: "#/components/schemas/GoogleServiceAccountAuth"
|
||||
VertexProviderOptions:
|
||||
type: object
|
||||
properties:
|
||||
provider:
|
||||
description: AI provider for the model (or provide a baseURL endpoint instead)
|
||||
example: openai
|
||||
project:
|
||||
description: Google Cloud project ID for Vertex AI models
|
||||
example: my-gcp-project
|
||||
type: string
|
||||
enum:
|
||||
- openai
|
||||
- anthropic
|
||||
- google
|
||||
- microsoft
|
||||
- bedrock
|
||||
- vertex
|
||||
location:
|
||||
description: Google Cloud location for Vertex AI models
|
||||
example: us-central1
|
||||
type: string
|
||||
baseURL:
|
||||
description: Base URL for the Vertex AI provider
|
||||
type: string
|
||||
format: uri
|
||||
headers:
|
||||
description: Custom headers sent with every request to the Vertex AI provider
|
||||
type: object
|
||||
propertyNames:
|
||||
type: string
|
||||
additionalProperties:
|
||||
type: string
|
||||
required:
|
||||
- project
|
||||
- location
|
||||
additionalProperties: false
|
||||
ModelProviderOptions:
|
||||
type: object
|
||||
properties:
|
||||
vertex:
|
||||
description: Vertex AI provider-specific settings
|
||||
$ref: "#/components/schemas/VertexProviderOptions"
|
||||
required:
|
||||
- vertex
|
||||
additionalProperties: false
|
||||
GenericModelConfigObject:
|
||||
type: object
|
||||
properties:
|
||||
modelName:
|
||||
description: Model name string with provider prefix (e.g., 'openai/gpt-5-nano')
|
||||
example: openai/gpt-5.4-mini
|
||||
@ -256,19 +291,62 @@ components:
|
||||
type: string
|
||||
additionalProperties:
|
||||
type: string
|
||||
project:
|
||||
description: Google Cloud project ID for Vertex AI models
|
||||
example: my-gcp-project
|
||||
provider:
|
||||
description: AI provider for the model (or provide a baseURL endpoint instead)
|
||||
example: openai
|
||||
type: string
|
||||
location:
|
||||
description: Google Cloud location for Vertex AI models
|
||||
example: us-central1
|
||||
type: string
|
||||
googleAuthOptions:
|
||||
description: google-auth-library options used to authenticate Vertex AI models
|
||||
$ref: "#/components/schemas/GoogleAuthOptions"
|
||||
enum:
|
||||
- openai
|
||||
- anthropic
|
||||
- google
|
||||
- microsoft
|
||||
- bedrock
|
||||
required:
|
||||
- modelName
|
||||
additionalProperties: false
|
||||
VertexModelConfigObject:
|
||||
type: object
|
||||
properties:
|
||||
modelName:
|
||||
description: Model name string with provider prefix (e.g., 'openai/gpt-5-nano')
|
||||
example: openai/gpt-5.4-mini
|
||||
type: string
|
||||
apiKey:
|
||||
description: API key for the model provider
|
||||
example: sk-some-openai-api-key
|
||||
type: string
|
||||
baseURL:
|
||||
description: Base URL for the model provider
|
||||
example: https://api.openai.com/v1
|
||||
type: string
|
||||
format: uri
|
||||
headers:
|
||||
description: Custom headers sent with every request to the model provider
|
||||
type: object
|
||||
propertyNames:
|
||||
type: string
|
||||
additionalProperties:
|
||||
type: string
|
||||
provider:
|
||||
description: Vertex AI model provider
|
||||
type: string
|
||||
const: vertex
|
||||
auth:
|
||||
description: Vertex provider authentication configuration
|
||||
$ref: "#/components/schemas/ModelAuth"
|
||||
providerOptions:
|
||||
description: Vertex provider-specific model configuration
|
||||
$ref: "#/components/schemas/ModelProviderOptions"
|
||||
required:
|
||||
- modelName
|
||||
- provider
|
||||
- auth
|
||||
- providerOptions
|
||||
additionalProperties: false
|
||||
ModelConfigObject:
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/VertexModelConfigObject"
|
||||
- $ref: "#/components/schemas/GenericModelConfigObject"
|
||||
ModelConfig:
|
||||
$ref: "#/components/schemas/ModelConfigObject"
|
||||
Action:
|
||||
@ -1319,56 +1397,6 @@ components:
|
||||
acceptDownloads:
|
||||
type: boolean
|
||||
additionalProperties: false
|
||||
ModelConfigObjectOutput:
|
||||
type: object
|
||||
properties:
|
||||
provider:
|
||||
description: AI provider for the model (or provide a baseURL endpoint instead)
|
||||
example: openai
|
||||
type: string
|
||||
enum:
|
||||
- openai
|
||||
- anthropic
|
||||
- google
|
||||
- microsoft
|
||||
- bedrock
|
||||
- vertex
|
||||
modelName:
|
||||
description: Model name string with provider prefix (e.g., 'openai/gpt-5-nano')
|
||||
example: openai/gpt-5.4-mini
|
||||
type: string
|
||||
apiKey:
|
||||
description: API key for the model provider
|
||||
example: sk-some-openai-api-key
|
||||
type: string
|
||||
baseURL:
|
||||
description: Base URL for the model provider
|
||||
example: https://api.openai.com/v1
|
||||
type: string
|
||||
format: uri
|
||||
headers:
|
||||
description: Custom headers sent with every request to the model provider
|
||||
type: object
|
||||
propertyNames:
|
||||
type: string
|
||||
additionalProperties:
|
||||
type: string
|
||||
project:
|
||||
description: Google Cloud project ID for Vertex AI models
|
||||
example: my-gcp-project
|
||||
type: string
|
||||
location:
|
||||
description: Google Cloud location for Vertex AI models
|
||||
example: us-central1
|
||||
type: string
|
||||
googleAuthOptions:
|
||||
description: google-auth-library options used to authenticate Vertex AI models
|
||||
$ref: "#/components/schemas/GoogleAuthOptions"
|
||||
required:
|
||||
- modelName
|
||||
additionalProperties: false
|
||||
ModelConfigOutput:
|
||||
$ref: "#/components/schemas/ModelConfigObjectOutput"
|
||||
ActionOutput:
|
||||
description: Action object returned by observe and used by act
|
||||
type: object
|
||||
@ -1633,7 +1661,7 @@ components:
|
||||
description: Model configuration object or model name string (e.g.,
|
||||
'openai/gpt-5-nano')
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ModelConfigOutput"
|
||||
- $ref: "#/components/schemas/ModelConfig"
|
||||
- type: string
|
||||
variables:
|
||||
description: Variables to substitute in the action instruction. Accepts flat
|
||||
@ -1693,7 +1721,7 @@ components:
|
||||
description: Model configuration object or model name string (e.g.,
|
||||
'openai/gpt-5-nano')
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ModelConfigOutput"
|
||||
- $ref: "#/components/schemas/ModelConfig"
|
||||
- type: string
|
||||
timeout:
|
||||
description: Timeout in ms for the extraction
|
||||
@ -1738,7 +1766,7 @@ components:
|
||||
description: Model configuration object or model name string (e.g.,
|
||||
'openai/gpt-5-nano')
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ModelConfigOutput"
|
||||
- $ref: "#/components/schemas/ModelConfig"
|
||||
- type: string
|
||||
variables:
|
||||
description: Variables whose names are exposed to the model so observe() returns
|
||||
@ -1801,7 +1829,7 @@ components:
|
||||
description: Model configuration object or model name string (e.g.,
|
||||
'openai/gpt-5-nano')
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ModelConfigOutput"
|
||||
- $ref: "#/components/schemas/ModelConfig"
|
||||
- type: string
|
||||
systemPrompt:
|
||||
description: Custom system prompt for the agent
|
||||
@ -1825,7 +1853,7 @@ components:
|
||||
agent tools). If not specified, inherits from the main model
|
||||
configuration.
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ModelConfigOutput"
|
||||
- $ref: "#/components/schemas/ModelConfig"
|
||||
- type: string
|
||||
additionalProperties: false
|
||||
AgentUsageOutput:
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
* SEA_INCLUDE_SOURCEMAPS.
|
||||
* Example: pnpm run build:sea:cjs -- --target-platform=linux --target-arch=arm64
|
||||
*/
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
@ -87,6 +87,17 @@ const includeSourcemaps = parseBoolean(
|
||||
false,
|
||||
);
|
||||
|
||||
const gitOutput = (args: string[]) =>
|
||||
execFileSync("git", args, { cwd: repoDir, encoding: "utf8" }).trim();
|
||||
const buildCommit = gitOutput(["rev-parse", "HEAD"]);
|
||||
const seaVersion = [
|
||||
`commit: ${buildCommit}`,
|
||||
`timestamp: ${gitOutput(["show", "-s", "--format=%cI", buildCommit])}`,
|
||||
].join("\n");
|
||||
const seaBuildDefine = {
|
||||
__STAGEHAND_SERVER_VERSION__: JSON.stringify(seaVersion),
|
||||
};
|
||||
|
||||
const run = (cmd: string, args: string[], opts: { cwd?: string } = {}) => {
|
||||
const result = spawnSync(cmd, args, { stdio: "inherit", ...opts });
|
||||
if (result.error) {
|
||||
@ -273,6 +284,7 @@ const buildCjsBundle = () => {
|
||||
platform: "node",
|
||||
format: "cjs",
|
||||
outfile: bundlePath,
|
||||
define: seaBuildDefine,
|
||||
logLevel: "warning",
|
||||
absWorkingDir: repoDir,
|
||||
});
|
||||
@ -295,6 +307,7 @@ const buildEsmBundle = () => {
|
||||
format: "esm",
|
||||
treeShaking: false,
|
||||
outfile: appBundlePath,
|
||||
define: seaBuildDefine,
|
||||
alias: {
|
||||
"@browserbasehq/stagehand": `${repoDir}/packages/core/dist/esm/index.js`,
|
||||
},
|
||||
@ -412,6 +425,15 @@ const { pathToFileURL } = require("node:url");
|
||||
const bundleBase64 = ${JSON.stringify(appBytes.toString("base64"))};
|
||||
const bundleLength = ${appBytes.length};
|
||||
const bundleHash = ${JSON.stringify(bundleHash)};
|
||||
const versionOutput = ${JSON.stringify(seaVersion)};
|
||||
|
||||
const argv = process.argv.slice(1);
|
||||
const normalizedArgv = argv[0]?.startsWith("--") ? argv : argv.slice(1);
|
||||
|
||||
if (normalizedArgv.includes("--version")) {
|
||||
console.log(versionOutput);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const cacheRoot =
|
||||
process.env.STAGEHAND_SEA_CACHE_DIR ||
|
||||
|
||||
@ -44,7 +44,12 @@ async function main() {
|
||||
LocalBrowserLaunchOptions: Api.LocalBrowserLaunchOptionsSchema,
|
||||
GoogleServiceAccountCredentials:
|
||||
Api.GoogleServiceAccountCredentialsSchema,
|
||||
GoogleAuthOptions: Api.GoogleAuthOptionsSchema,
|
||||
GoogleServiceAccountAuth: Api.GoogleServiceAccountAuthSchema,
|
||||
ModelAuth: Api.ModelAuthSchema,
|
||||
VertexProviderOptions: Api.VertexProviderOptionsSchema,
|
||||
ModelProviderOptions: Api.ModelProviderOptionsSchema,
|
||||
GenericModelConfigObject: Api.GenericModelConfigObjectSchema,
|
||||
VertexModelConfigObject: Api.VertexModelConfigObjectSchema,
|
||||
ModelConfigObject: Api.ModelConfigObjectSchema,
|
||||
ModelConfig: Api.ModelConfigSchema,
|
||||
Action: Api.ActionSchema,
|
||||
|
||||
@ -208,10 +208,17 @@ export class InMemorySessionStore implements SessionStore {
|
||||
|
||||
const options: V3Options = {
|
||||
env: isBrowserbase ? "BROWSERBASE" : "LOCAL",
|
||||
model: {
|
||||
modelName: params.modelName,
|
||||
apiKey: ctx.modelApiKey,
|
||||
},
|
||||
model: ctx.requestModelConfig
|
||||
? {
|
||||
...ctx.requestModelConfig,
|
||||
...(ctx.requestModelConfig.apiKey
|
||||
? {}
|
||||
: { apiKey: ctx.modelApiKey }),
|
||||
}
|
||||
: {
|
||||
modelName: params.modelName,
|
||||
apiKey: ctx.modelApiKey,
|
||||
},
|
||||
verbose: params.verbose,
|
||||
systemPrompt: params.systemPrompt,
|
||||
selfHeal: params.selfHeal,
|
||||
|
||||
@ -67,6 +67,8 @@ export interface CreateSessionParams {
|
||||
export interface RequestContext {
|
||||
/** Model API key (from x-model-api-key header) */
|
||||
modelApiKey?: string;
|
||||
/** Structured model config from the current request body */
|
||||
requestModelConfig?: Api.ModelConfig;
|
||||
/** Logger function for this request */
|
||||
logger?: (message: LogLine) => void;
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { Api } from "@browserbasehq/stagehand";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { MissingHeaderError } from "../types/error.js";
|
||||
|
||||
@ -39,41 +41,134 @@ export const getOptionalHeader = (
|
||||
return headerValue;
|
||||
};
|
||||
|
||||
const emptyStringSchema = z.literal("").transform((): undefined => undefined);
|
||||
const requiredStringSchema = z.string().min(1);
|
||||
const optionalStringSchema = z
|
||||
.union([emptyStringSchema, z.string()])
|
||||
.optional();
|
||||
|
||||
const requestModelInputSchema = z
|
||||
.union([
|
||||
emptyStringSchema,
|
||||
requiredStringSchema.transform(
|
||||
(modelName): Api.ModelConfig => ({ modelName }),
|
||||
),
|
||||
Api.ModelConfigSchema,
|
||||
])
|
||||
.optional();
|
||||
const requestModelCarrierSchema = z
|
||||
.object({
|
||||
model: requestModelInputSchema,
|
||||
})
|
||||
.passthrough();
|
||||
const unparsedModelCarrierSchema = z
|
||||
.object({
|
||||
model: z.unknown().optional(),
|
||||
})
|
||||
.passthrough();
|
||||
const requestModelEnvelopeSchema = z
|
||||
.object({
|
||||
options: requestModelCarrierSchema.optional(),
|
||||
agentConfig: unparsedModelCarrierSchema.optional(),
|
||||
modelName: optionalStringSchema,
|
||||
})
|
||||
.passthrough();
|
||||
const stagehandInitModelEnvelopeSchema = z
|
||||
.object({
|
||||
options: requestModelCarrierSchema.optional(),
|
||||
agentConfig: requestModelCarrierSchema.optional(),
|
||||
modelName: optionalStringSchema,
|
||||
})
|
||||
.passthrough();
|
||||
const requestModelConfigSchema = z
|
||||
.object({
|
||||
model: Api.ModelConfigSchema.optional(),
|
||||
modelName: optionalStringSchema,
|
||||
apiKey: optionalStringSchema,
|
||||
})
|
||||
.strict();
|
||||
|
||||
export type RequestModelConfig = z.infer<typeof requestModelConfigSchema>;
|
||||
type RequestModelConfigResult =
|
||||
| { success: true; data: RequestModelConfig }
|
||||
| { success: false; error: z.ZodError };
|
||||
|
||||
/**
|
||||
* Extracts model name from request body, supporting V3 structure.
|
||||
* - V3: body.options.model.modelName
|
||||
* Extracts request-level model config with precedence.
|
||||
*
|
||||
* Model name:
|
||||
* 1. body.options.model.modelName or body.options.model string
|
||||
* 2. Legacy body.modelName fallback
|
||||
*
|
||||
* API key:
|
||||
* 1. body.options.model.apiKey
|
||||
* 2. x-model-api-key header
|
||||
*
|
||||
* agentConfig.model is parsed separately for Stagehand initialization. Its
|
||||
* credentials are scoped to the agent main model and must not become the
|
||||
* request-level API key fallback used by action/execution models.
|
||||
*/
|
||||
export function getModelName(request: FastifyRequest): string | undefined {
|
||||
const body = request.body as Record<string, unknown> | undefined;
|
||||
const options = body?.options as Record<string, unknown> | undefined;
|
||||
const model = options?.model as Record<string, unknown> | undefined;
|
||||
|
||||
if (typeof model?.modelName === "string" && model.modelName) {
|
||||
return model.modelName;
|
||||
export function getRequestModelConfig(
|
||||
request: FastifyRequest,
|
||||
): RequestModelConfigResult {
|
||||
const bodyResult = requestModelEnvelopeSchema.safeParse(request.body ?? {});
|
||||
if (!bodyResult.success) {
|
||||
return bodyResult;
|
||||
}
|
||||
|
||||
if (typeof body?.modelName === "string" && body.modelName) {
|
||||
return body.modelName;
|
||||
const body = bodyResult.data;
|
||||
const model = body.options?.model;
|
||||
const configResult = requestModelConfigSchema.safeParse({
|
||||
model,
|
||||
modelName: model?.modelName ?? body.modelName,
|
||||
apiKey: model?.apiKey ?? getOptionalHeader(request, "x-model-api-key"),
|
||||
});
|
||||
if (!configResult.success) {
|
||||
return configResult;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
return configResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the model API key with precedence:
|
||||
* 1. Per-request body apiKey (V3: body.options.model.apiKey)
|
||||
* 2. Per-request header x-model-api-key
|
||||
* Extracts the structured model config used when creating a Stagehand instance
|
||||
* for this request. This can read agentConfig.model for agentExecute startup,
|
||||
* but it does not promote agent model credentials to the request-level API key.
|
||||
*/
|
||||
export function getModelApiKey(request: FastifyRequest): string | undefined {
|
||||
const body = request.body as Record<string, unknown> | undefined;
|
||||
const options = body?.options as Record<string, unknown> | undefined;
|
||||
const model = options?.model as Record<string, unknown> | undefined;
|
||||
|
||||
if (typeof model?.apiKey === "string" && model.apiKey) {
|
||||
return model.apiKey;
|
||||
export function getStagehandInitModelConfig(
|
||||
request: FastifyRequest,
|
||||
requestModelConfig?: RequestModelConfig,
|
||||
): RequestModelConfigResult {
|
||||
const baseConfigResult = requestModelConfig
|
||||
? requestModelConfigSchema.safeParse(requestModelConfig)
|
||||
: getRequestModelConfig(request);
|
||||
if (!baseConfigResult.success) {
|
||||
return baseConfigResult;
|
||||
}
|
||||
|
||||
return getOptionalHeader(request, "x-model-api-key");
|
||||
const baseConfig = baseConfigResult.data;
|
||||
if (baseConfig.model) {
|
||||
return baseConfigResult;
|
||||
}
|
||||
|
||||
const bodyResult = stagehandInitModelEnvelopeSchema.safeParse(
|
||||
request.body ?? {},
|
||||
);
|
||||
if (!bodyResult.success) {
|
||||
return bodyResult;
|
||||
}
|
||||
|
||||
const agentModel = bodyResult.data.agentConfig?.model;
|
||||
const configResult = requestModelConfigSchema.safeParse({
|
||||
model: agentModel,
|
||||
modelName: agentModel?.modelName ?? baseConfig.modelName,
|
||||
apiKey: baseConfig.apiKey,
|
||||
});
|
||||
if (!configResult.success) {
|
||||
return configResult;
|
||||
}
|
||||
|
||||
return configResult;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -6,10 +6,11 @@ import { z } from "zod/v4";
|
||||
|
||||
import { AppError } from "./errorHandler.js";
|
||||
import {
|
||||
getModelApiKey,
|
||||
getModelName,
|
||||
getOptionalHeader,
|
||||
getRequestModelConfig,
|
||||
getStagehandInitModelConfig,
|
||||
shouldRespondWithSSE,
|
||||
type RequestModelConfig,
|
||||
} from "./header.js";
|
||||
import { error, success } from "./response.js";
|
||||
import { getSessionStore } from "./sessionStoreManager.js";
|
||||
@ -35,6 +36,13 @@ type StreamEventName =
|
||||
| "error";
|
||||
type StreamPayloadType = "system" | "log";
|
||||
|
||||
function formatZodIssues(err: z.ZodError) {
|
||||
return err.issues.map((issue) => ({
|
||||
path: issue.path[0] ?? "unknown",
|
||||
message: issue.message,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function createStreamingResponse<TV3>({
|
||||
sessionId,
|
||||
request,
|
||||
@ -44,7 +52,6 @@ export async function createStreamingResponse<TV3>({
|
||||
operation,
|
||||
}: StreamingResponseOptions<TV3>) {
|
||||
const shouldStreamResponse = shouldRespondWithSSE(request);
|
||||
const modelApiKey = getModelApiKey(request);
|
||||
|
||||
const sessionStore = getSessionStore();
|
||||
const sessionConfig = await sessionStore.getSessionConfig(sessionId);
|
||||
@ -127,9 +134,45 @@ export async function createStreamingResponse<TV3>({
|
||||
const actionId = v4();
|
||||
|
||||
sendData("starting", "system", { status: "starting" });
|
||||
const sendZodValidationError = (err: z.ZodError) => {
|
||||
const validationIssues = formatZodIssues(err);
|
||||
if (shouldStreamResponse) {
|
||||
sendData("error", "system", {
|
||||
status: "error",
|
||||
error: validationIssues,
|
||||
});
|
||||
reply.raw.end();
|
||||
return reply;
|
||||
}
|
||||
|
||||
return reply.status(StatusCodes.BAD_REQUEST).send({
|
||||
error: validationIssues,
|
||||
});
|
||||
};
|
||||
|
||||
const requestModelConfigResult = getRequestModelConfig(request);
|
||||
if (requestModelConfigResult.success === false) {
|
||||
return sendZodValidationError(requestModelConfigResult.error);
|
||||
}
|
||||
|
||||
const requestModelConfig: RequestModelConfig = requestModelConfigResult.data;
|
||||
const stagehandInitModelConfigResult = getStagehandInitModelConfig(
|
||||
request,
|
||||
requestModelConfig,
|
||||
);
|
||||
if (stagehandInitModelConfigResult.success === false) {
|
||||
return sendZodValidationError(stagehandInitModelConfigResult.error);
|
||||
}
|
||||
|
||||
const stagehandInitModelConfig = stagehandInitModelConfigResult.data;
|
||||
const modelApiKey = requestModelConfig.apiKey;
|
||||
const parsedRequestModelConfig = stagehandInitModelConfig.model?.modelName
|
||||
? stagehandInitModelConfig.model
|
||||
: undefined;
|
||||
|
||||
const requestContext: RequestContext = {
|
||||
modelApiKey,
|
||||
requestModelConfig: parsedRequestModelConfig,
|
||||
logger: shouldStreamResponse
|
||||
? (message) => {
|
||||
sendData("running", "log", { status: "running", message });
|
||||
@ -177,7 +220,7 @@ export async function createStreamingResponse<TV3>({
|
||||
operation: operation ?? "operation",
|
||||
sessionId,
|
||||
browserType,
|
||||
modelName: getModelName(request),
|
||||
modelName: requestModelConfig.modelName,
|
||||
hasModelApiKey: Boolean(modelApiKey),
|
||||
hasBrowserbaseApiKey: Boolean(browserbaseApiKey),
|
||||
hasBrowserbaseProjectId: Boolean(browserbaseProjectId),
|
||||
|
||||
@ -8,7 +8,10 @@ import { z } from "zod/v4";
|
||||
|
||||
import { authMiddleware } from "../../../lib/auth.js";
|
||||
import { withErrorHandling } from "../../../lib/errorHandler.js";
|
||||
import { getModelApiKey, getOptionalHeader } from "../../../lib/header.js";
|
||||
import {
|
||||
getOptionalHeader,
|
||||
getRequestModelConfig,
|
||||
} from "../../../lib/header.js";
|
||||
import { error, success } from "../../../lib/response.js";
|
||||
import { getSessionStore } from "../../../lib/sessionStoreManager.js";
|
||||
import { AISDK_PROVIDERS } from "../../../types/model.js";
|
||||
@ -110,11 +113,30 @@ const startRouteHandler: RouteHandler = withErrorHandling(
|
||||
}
|
||||
|
||||
const browserType = browser?.type ?? "browserbase";
|
||||
const localBrowserLaunchOptions =
|
||||
browserType === "local" && browser?.launchOptions && !browser?.cdpUrl
|
||||
? browser.launchOptions
|
||||
: undefined;
|
||||
|
||||
let bbApiKey: string | undefined;
|
||||
let browserbaseProjectId: string | undefined;
|
||||
let browserbaseSessionId: string | undefined;
|
||||
let connectUrl: string | undefined;
|
||||
let localBrowserModelApiKey: string | undefined;
|
||||
|
||||
if (localBrowserLaunchOptions) {
|
||||
const requestModelConfigResult = getRequestModelConfig(request);
|
||||
if (requestModelConfigResult.success === false) {
|
||||
return reply.status(StatusCodes.BAD_REQUEST).send({
|
||||
error: requestModelConfigResult.error.issues.map((issue) => ({
|
||||
path: issue.path[0] ?? "unknown",
|
||||
message: issue.message,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
localBrowserModelApiKey = requestModelConfigResult.data.apiKey;
|
||||
}
|
||||
|
||||
if (browserType === "browserbase") {
|
||||
bbApiKey = getOptionalHeader(request, "x-bb-api-key");
|
||||
@ -210,12 +232,11 @@ const startRouteHandler: RouteHandler = withErrorHandling(
|
||||
// For local browsers with launchOptions (no explicit cdpUrl), eagerly
|
||||
// initialize the browser so we can return the actual CDP URL
|
||||
let finalCdpUrl = connectUrl ?? session.cdpUrl ?? "";
|
||||
if (browserType === "local" && browser?.launchOptions && !browser?.cdpUrl) {
|
||||
const modelApiKey = getModelApiKey(request);
|
||||
if (localBrowserLaunchOptions) {
|
||||
try {
|
||||
const stagehand = await sessionStore.getOrCreateStagehand(
|
||||
session.sessionId,
|
||||
{ modelApiKey },
|
||||
{ modelApiKey: localBrowserModelApiKey },
|
||||
);
|
||||
finalCdpUrl = stagehand.connectURL();
|
||||
} catch (err) {
|
||||
@ -226,12 +247,12 @@ const startRouteHandler: RouteHandler = withErrorHandling(
|
||||
browserType,
|
||||
chromePathEnv: process.env.CHROME_PATH,
|
||||
launchOptions: {
|
||||
executablePath: browser.launchOptions.executablePath,
|
||||
argsCount: browser.launchOptions.args?.length ?? 0,
|
||||
headless: browser.launchOptions.headless,
|
||||
hasUserDataDir: Boolean(browser.launchOptions.userDataDir),
|
||||
port: browser.launchOptions.port,
|
||||
connectTimeoutMs: browser.launchOptions.connectTimeoutMs,
|
||||
executablePath: localBrowserLaunchOptions.executablePath,
|
||||
argsCount: localBrowserLaunchOptions.args?.length ?? 0,
|
||||
headless: localBrowserLaunchOptions.headless,
|
||||
hasUserDataDir: Boolean(localBrowserLaunchOptions.userDataDir),
|
||||
port: localBrowserLaunchOptions.port,
|
||||
connectTimeoutMs: localBrowserLaunchOptions.connectTimeoutMs,
|
||||
},
|
||||
},
|
||||
"Failed to initialize local browser session in /v1/sessions/start",
|
||||
|
||||
@ -1,15 +1,23 @@
|
||||
import { __internalMaybeRunShutdownSupervisorFromArgv } from "@browserbasehq/stagehand";
|
||||
declare const __STAGEHAND_SERVER_VERSION__: string;
|
||||
|
||||
// if SEA binary is launched with --supervisor, it will run the shutdown supervisor only
|
||||
const argv = process.argv.slice(1);
|
||||
const normalizedArgv = argv[0]?.startsWith("--") ? argv : argv.slice(1);
|
||||
|
||||
// otherwise, start the stagehand/server
|
||||
if (!__internalMaybeRunShutdownSupervisorFromArgv(normalizedArgv)) {
|
||||
// Force flow logs on before the server module loads Stagehand core.
|
||||
process.env.BROWSERBASE_FLOW_LOGS = "1";
|
||||
void import("./server.js").catch((err) => {
|
||||
if (normalizedArgv.includes("--version")) {
|
||||
console.log(__STAGEHAND_SERVER_VERSION__);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
void import("@browserbasehq/stagehand")
|
||||
.then(async ({ __internalMaybeRunShutdownSupervisorFromArgv }) => {
|
||||
if (__internalMaybeRunShutdownSupervisorFromArgv(normalizedArgv)) {
|
||||
return;
|
||||
}
|
||||
|
||||
process.env.BROWSERBASE_FLOW_LOGS = "1";
|
||||
await import("./server.js");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed to start server:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
120
packages/server-v3/tests/unit/requestModelConfig.test.ts
Normal file
120
packages/server-v3/tests/unit/requestModelConfig.test.ts
Normal file
@ -0,0 +1,120 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
|
||||
import type { FastifyRequest } from "fastify";
|
||||
|
||||
import {
|
||||
getRequestModelConfig,
|
||||
getStagehandInitModelConfig,
|
||||
type RequestModelConfig,
|
||||
} from "../../src/lib/header.js";
|
||||
|
||||
function createRequest({
|
||||
body,
|
||||
headers = {},
|
||||
}: {
|
||||
body?: Record<string, unknown>;
|
||||
headers?: Record<string, string>;
|
||||
}): FastifyRequest {
|
||||
return {
|
||||
body,
|
||||
headers,
|
||||
} as FastifyRequest;
|
||||
}
|
||||
|
||||
function assertSuccess(
|
||||
result: ReturnType<typeof getRequestModelConfig>,
|
||||
): RequestModelConfig {
|
||||
if (result.success === false) {
|
||||
throw result.error;
|
||||
}
|
||||
assert.equal(result.success, true);
|
||||
return result.data;
|
||||
}
|
||||
|
||||
describe("getRequestModelConfig", () => {
|
||||
it("preserves a Vertex model config from an action request so auth fields reach session initialization", () => {
|
||||
const model = {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
auth: {
|
||||
type: "googleServiceAccount",
|
||||
credentials: {
|
||||
client_email: "vertex@example.iam.gserviceaccount.com",
|
||||
private_key:
|
||||
"-----BEGIN PRIVATE KEY-----\\ntest\\n-----END PRIVATE KEY-----",
|
||||
},
|
||||
},
|
||||
providerOptions: {
|
||||
vertex: {
|
||||
project: "test-gcp-project",
|
||||
location: "us-central1",
|
||||
},
|
||||
},
|
||||
};
|
||||
const request = createRequest({
|
||||
body: {
|
||||
options: { model },
|
||||
},
|
||||
headers: {
|
||||
"x-model-api-key": "sk-header",
|
||||
},
|
||||
});
|
||||
|
||||
const config = assertSuccess(getRequestModelConfig(request));
|
||||
|
||||
assert.equal(config.modelName, "vertex/gemini-2.5-flash");
|
||||
assert.equal(config.apiKey, "sk-header");
|
||||
assert.deepEqual(config.model, model);
|
||||
});
|
||||
|
||||
it("does not read agentConfig.model as a request-level model config", () => {
|
||||
const request = createRequest({
|
||||
body: {
|
||||
agentConfig: { model: "google/gemini-2.5-flash" },
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(assertSuccess(getRequestModelConfig(request)), {
|
||||
model: undefined,
|
||||
modelName: undefined,
|
||||
apiKey: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes an agent string model into the session bootstrap model config", () => {
|
||||
const request = createRequest({
|
||||
body: {
|
||||
agentConfig: { model: "google/gemini-2.5-flash" },
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(assertSuccess(getStagehandInitModelConfig(request)), {
|
||||
model: { modelName: "google/gemini-2.5-flash" },
|
||||
modelName: "google/gemini-2.5-flash",
|
||||
apiKey: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns a validation result instead of throwing when agent bootstrap model config is invalid", () => {
|
||||
const request = createRequest({
|
||||
body: {
|
||||
agentConfig: {
|
||||
model: {
|
||||
provider: "vertex",
|
||||
modelName: "vertex/gemini-2.5-flash",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(assertSuccess(getRequestModelConfig(request)), {
|
||||
model: undefined,
|
||||
modelName: undefined,
|
||||
apiKey: undefined,
|
||||
});
|
||||
|
||||
const result = getStagehandInitModelConfig(request);
|
||||
assert.equal(result.success, false);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user