File tree 4 files changed +44
-1
lines changed
4 files changed +44
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @browserbasehq/stagehand " : minor
3
+ ---
4
+
5
+ Added support for offloading agent tasks to the API.
Original file line number Diff line number Diff line change @@ -12,11 +12,13 @@ import { GotoOptions } from "../types/playwright";
12
12
import {
13
13
ActOptions ,
14
14
ActResult ,
15
+ AgentConfig ,
15
16
ExtractOptions ,
16
17
ExtractResult ,
17
18
ObserveOptions ,
18
19
ObserveResult ,
19
20
} from "../types/stagehand" ;
21
+ import { AgentExecuteOptions , AgentResult } from "." ;
20
22
21
23
export class StagehandAPI {
22
24
private apiKey : string ;
@@ -118,6 +120,16 @@ export class StagehandAPI {
118
120
} ) ;
119
121
}
120
122
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
+
121
133
async end ( ) : Promise < Response > {
122
134
const url = `/sessions/${ this . sessionId } /end` ;
123
135
return await this . request ( url , {
Original file line number Diff line number Diff line change @@ -851,6 +851,32 @@ export class Stagehand {
851
851
throw new Error ( "Instruction is required for agent execution" ) ;
852
852
}
853
853
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
+
854
880
return await agentHandler . execute ( executeOptions ) ;
855
881
} ,
856
882
} ;
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ export interface StagehandAPIConstructorParams {
8
8
}
9
9
10
10
export interface ExecuteActionParams {
11
- method : "act" | "extract" | "observe" | "navigate" | "end" ;
11
+ method : "act" | "extract" | "observe" | "navigate" | "end" | "agentExecute" ;
12
12
args ?: unknown ;
13
13
params ?: unknown ;
14
14
}
You can’t perform that action at this time.
0 commit comments