Skip to content

Commit ba9efc5

Browse files
authored
add agent API support (#588)
* offload agent to api * changeset * auto-add api key to options
1 parent 0c4b1e7 commit ba9efc5

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

Diff for: .changeset/curly-rules-build.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
Added support for offloading agent tasks to the API.

Diff for: lib/api.ts

+12
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import { GotoOptions } from "../types/playwright";
1212
import {
1313
ActOptions,
1414
ActResult,
15+
AgentConfig,
1516
ExtractOptions,
1617
ExtractResult,
1718
ObserveOptions,
1819
ObserveResult,
1920
} from "../types/stagehand";
21+
import { AgentExecuteOptions, AgentResult } from ".";
2022

2123
export class StagehandAPI {
2224
private apiKey: string;
@@ -118,6 +120,16 @@ export class StagehandAPI {
118120
});
119121
}
120122

123+
async agentExecute(
124+
agentConfig: AgentConfig,
125+
executeOptions: AgentExecuteOptions,
126+
): Promise<AgentResult> {
127+
return this.execute<AgentResult>({
128+
method: "agentExecute",
129+
args: { agentConfig, executeOptions },
130+
});
131+
}
132+
121133
async end(): Promise<Response> {
122134
const url = `/sessions/${this.sessionId}/end`;
123135
return await this.request(url, {

Diff for: lib/index.ts

+26
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,32 @@ export class Stagehand {
851851
throw new Error("Instruction is required for agent execution");
852852
}
853853

854+
if (this.usingAPI) {
855+
if (!this.apiClient) {
856+
throw new Error(
857+
"API client not initialized. Ensure that you have initialized Stagehand via `await stagehand.init()`.",
858+
);
859+
}
860+
861+
if (!options.options) {
862+
options.options = {};
863+
}
864+
865+
if (options.provider === "anthropic") {
866+
options.options.apiKey = process.env.ANTHROPIC_API_KEY;
867+
} else if (options.provider === "openai") {
868+
options.options.apiKey = process.env.OPENAI_API_KEY;
869+
}
870+
871+
if (!options.options.apiKey) {
872+
throw new Error(
873+
`API key not found for \`${options.provider}\` provider. Please set the ${options.provider === "anthropic" ? "ANTHROPIC_API_KEY" : "OPENAI_API_KEY"} environment variable or pass an apiKey in the options object.`,
874+
);
875+
}
876+
877+
return await this.apiClient.agentExecute(options, executeOptions);
878+
}
879+
854880
return await agentHandler.execute(executeOptions);
855881
},
856882
};

Diff for: types/api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface StagehandAPIConstructorParams {
88
}
99

1010
export interface ExecuteActionParams {
11-
method: "act" | "extract" | "observe" | "navigate" | "end";
11+
method: "act" | "extract" | "observe" | "navigate" | "end" | "agentExecute";
1212
args?: unknown;
1313
params?: unknown;
1414
}

0 commit comments

Comments
 (0)