Skip to content

Commit 1a0a44c

Browse files
feat: remove experimental requirement on agent variables (#2079)
1 parent 4700262 commit 1a0a44c

5 files changed

Lines changed: 207 additions & 5 deletions

File tree

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 8
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-f10429ab9f004c9a7f9f362d7fa82915e0dc04b1ba08a7bc9fd442ed5854cc77.yml
3-
openapi_spec_hash: 818f2e6e7eb5eb6f21f346b0b9828dce
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-6f6bfb81d092f30a5e2005328c97d61b9ea36132bb19e9e79e55294b9534ce20.yml
3+
openapi_spec_hash: f3fc1e3688a38dc2c28f7178f7d534e5
44
config_hash: 1fb12ae9b478488bc1e56bfbdc210b01

stagehand-java-core/src/main/kotlin/com/browserbase/api/models/sessions/SessionExecuteParams.kt

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.browserbase.api.core.checkRequired
1515
import com.browserbase.api.core.getOrThrow
1616
import com.browserbase.api.core.http.Headers
1717
import com.browserbase.api.core.http.QueryParams
18+
import com.browserbase.api.core.toImmutable
1819
import com.browserbase.api.errors.StagehandInvalidDataException
1920
import com.fasterxml.jackson.annotation.JsonAnyGetter
2021
import com.fasterxml.jackson.annotation.JsonAnySetter
@@ -1806,6 +1807,7 @@ private constructor(
18061807
private val maxSteps: JsonField<Double>,
18071808
private val toolTimeout: JsonField<Double>,
18081809
private val useSearch: JsonField<Boolean>,
1810+
private val variables: JsonField<Variables>,
18091811
private val additionalProperties: MutableMap<String, JsonValue>,
18101812
) {
18111813

@@ -1826,7 +1828,18 @@ private constructor(
18261828
@JsonProperty("useSearch")
18271829
@ExcludeMissing
18281830
useSearch: JsonField<Boolean> = JsonMissing.of(),
1829-
) : this(instruction, highlightCursor, maxSteps, toolTimeout, useSearch, mutableMapOf())
1831+
@JsonProperty("variables")
1832+
@ExcludeMissing
1833+
variables: JsonField<Variables> = JsonMissing.of(),
1834+
) : this(
1835+
instruction,
1836+
highlightCursor,
1837+
maxSteps,
1838+
toolTimeout,
1839+
useSearch,
1840+
variables,
1841+
mutableMapOf(),
1842+
)
18301843

18311844
/**
18321845
* Natural language instruction for the agent
@@ -1868,6 +1881,14 @@ private constructor(
18681881
*/
18691882
fun useSearch(): Optional<Boolean> = useSearch.getOptional("useSearch")
18701883

1884+
/**
1885+
* Variables available to the agent via %variableName% syntax in supported tools
1886+
*
1887+
* @throws StagehandInvalidDataException if the JSON field has an unexpected type (e.g. if
1888+
* the server responded with an unexpected value).
1889+
*/
1890+
fun variables(): Optional<Variables> = variables.getOptional("variables")
1891+
18711892
/**
18721893
* Returns the raw JSON value of [instruction].
18731894
*
@@ -1910,6 +1931,15 @@ private constructor(
19101931
*/
19111932
@JsonProperty("useSearch") @ExcludeMissing fun _useSearch(): JsonField<Boolean> = useSearch
19121933

1934+
/**
1935+
* Returns the raw JSON value of [variables].
1936+
*
1937+
* Unlike [variables], this method doesn't throw if the JSON field has an unexpected type.
1938+
*/
1939+
@JsonProperty("variables")
1940+
@ExcludeMissing
1941+
fun _variables(): JsonField<Variables> = variables
1942+
19131943
@JsonAnySetter
19141944
private fun putAdditionalProperty(key: String, value: JsonValue) {
19151945
additionalProperties.put(key, value)
@@ -1943,6 +1973,7 @@ private constructor(
19431973
private var maxSteps: JsonField<Double> = JsonMissing.of()
19441974
private var toolTimeout: JsonField<Double> = JsonMissing.of()
19451975
private var useSearch: JsonField<Boolean> = JsonMissing.of()
1976+
private var variables: JsonField<Variables> = JsonMissing.of()
19461977
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
19471978

19481979
@JvmSynthetic
@@ -1952,6 +1983,7 @@ private constructor(
19521983
maxSteps = executeOptions.maxSteps
19531984
toolTimeout = executeOptions.toolTimeout
19541985
useSearch = executeOptions.useSearch
1986+
variables = executeOptions.variables
19551987
additionalProperties = executeOptions.additionalProperties.toMutableMap()
19561988
}
19571989

@@ -2022,6 +2054,18 @@ private constructor(
20222054
*/
20232055
fun useSearch(useSearch: JsonField<Boolean>) = apply { this.useSearch = useSearch }
20242056

2057+
/** Variables available to the agent via %variableName% syntax in supported tools */
2058+
fun variables(variables: Variables) = variables(JsonField.of(variables))
2059+
2060+
/**
2061+
* Sets [Builder.variables] to an arbitrary JSON value.
2062+
*
2063+
* You should usually call [Builder.variables] with a well-typed [Variables] value
2064+
* instead. This method is primarily for setting the field to an undocumented or not yet
2065+
* supported value.
2066+
*/
2067+
fun variables(variables: JsonField<Variables>) = apply { this.variables = variables }
2068+
20252069
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
20262070
this.additionalProperties.clear()
20272071
putAllAdditionalProperties(additionalProperties)
@@ -2060,6 +2104,7 @@ private constructor(
20602104
maxSteps,
20612105
toolTimeout,
20622106
useSearch,
2107+
variables,
20632108
additionalProperties.toMutableMap(),
20642109
)
20652110
}
@@ -2085,6 +2130,7 @@ private constructor(
20852130
maxSteps()
20862131
toolTimeout()
20872132
useSearch()
2133+
variables().ifPresent { it.validate() }
20882134
validated = true
20892135
}
20902136

@@ -2108,7 +2154,121 @@ private constructor(
21082154
(if (highlightCursor.asKnown().isPresent) 1 else 0) +
21092155
(if (maxSteps.asKnown().isPresent) 1 else 0) +
21102156
(if (toolTimeout.asKnown().isPresent) 1 else 0) +
2111-
(if (useSearch.asKnown().isPresent) 1 else 0)
2157+
(if (useSearch.asKnown().isPresent) 1 else 0) +
2158+
(variables.asKnown().getOrNull()?.validity() ?: 0)
2159+
2160+
/** Variables available to the agent via %variableName% syntax in supported tools */
2161+
class Variables
2162+
@JsonCreator
2163+
private constructor(
2164+
@com.fasterxml.jackson.annotation.JsonValue
2165+
private val additionalProperties: Map<String, JsonValue>
2166+
) {
2167+
2168+
@JsonAnyGetter
2169+
@ExcludeMissing
2170+
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
2171+
2172+
fun toBuilder() = Builder().from(this)
2173+
2174+
companion object {
2175+
2176+
/** Returns a mutable builder for constructing an instance of [Variables]. */
2177+
@JvmStatic fun builder() = Builder()
2178+
}
2179+
2180+
/** A builder for [Variables]. */
2181+
class Builder internal constructor() {
2182+
2183+
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
2184+
2185+
@JvmSynthetic
2186+
internal fun from(variables: Variables) = apply {
2187+
additionalProperties = variables.additionalProperties.toMutableMap()
2188+
}
2189+
2190+
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
2191+
this.additionalProperties.clear()
2192+
putAllAdditionalProperties(additionalProperties)
2193+
}
2194+
2195+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
2196+
additionalProperties.put(key, value)
2197+
}
2198+
2199+
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) =
2200+
apply {
2201+
this.additionalProperties.putAll(additionalProperties)
2202+
}
2203+
2204+
fun removeAdditionalProperty(key: String) = apply {
2205+
additionalProperties.remove(key)
2206+
}
2207+
2208+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
2209+
keys.forEach(::removeAdditionalProperty)
2210+
}
2211+
2212+
/**
2213+
* Returns an immutable instance of [Variables].
2214+
*
2215+
* Further updates to this [Builder] will not mutate the returned instance.
2216+
*/
2217+
fun build(): Variables = Variables(additionalProperties.toImmutable())
2218+
}
2219+
2220+
private var validated: Boolean = false
2221+
2222+
/**
2223+
* Validates that the types of all values in this object match their expected types
2224+
* recursively.
2225+
*
2226+
* This method is _not_ forwards compatible with new types from the API for existing
2227+
* fields.
2228+
*
2229+
* @throws StagehandInvalidDataException if any value type in this object doesn't match
2230+
* its expected type.
2231+
*/
2232+
fun validate(): Variables = apply {
2233+
if (validated) {
2234+
return@apply
2235+
}
2236+
2237+
validated = true
2238+
}
2239+
2240+
fun isValid(): Boolean =
2241+
try {
2242+
validate()
2243+
true
2244+
} catch (e: StagehandInvalidDataException) {
2245+
false
2246+
}
2247+
2248+
/**
2249+
* Returns a score indicating how many valid values are contained in this object
2250+
* recursively.
2251+
*
2252+
* Used for best match union deserialization.
2253+
*/
2254+
@JvmSynthetic
2255+
internal fun validity(): Int =
2256+
additionalProperties.count { (_, value) -> !value.isNull() && !value.isMissing() }
2257+
2258+
override fun equals(other: Any?): Boolean {
2259+
if (this === other) {
2260+
return true
2261+
}
2262+
2263+
return other is Variables && additionalProperties == other.additionalProperties
2264+
}
2265+
2266+
private val hashCode: Int by lazy { Objects.hash(additionalProperties) }
2267+
2268+
override fun hashCode(): Int = hashCode
2269+
2270+
override fun toString() = "Variables{additionalProperties=$additionalProperties}"
2271+
}
21122272

21132273
override fun equals(other: Any?): Boolean {
21142274
if (this === other) {
@@ -2121,6 +2281,7 @@ private constructor(
21212281
maxSteps == other.maxSteps &&
21222282
toolTimeout == other.toolTimeout &&
21232283
useSearch == other.useSearch &&
2284+
variables == other.variables &&
21242285
additionalProperties == other.additionalProperties
21252286
}
21262287

@@ -2131,14 +2292,15 @@ private constructor(
21312292
maxSteps,
21322293
toolTimeout,
21332294
useSearch,
2295+
variables,
21342296
additionalProperties,
21352297
)
21362298
}
21372299

21382300
override fun hashCode(): Int = hashCode
21392301

21402302
override fun toString() =
2141-
"ExecuteOptions{instruction=$instruction, highlightCursor=$highlightCursor, maxSteps=$maxSteps, toolTimeout=$toolTimeout, useSearch=$useSearch, additionalProperties=$additionalProperties}"
2303+
"ExecuteOptions{instruction=$instruction, highlightCursor=$highlightCursor, maxSteps=$maxSteps, toolTimeout=$toolTimeout, useSearch=$useSearch, variables=$variables, additionalProperties=$additionalProperties}"
21422304
}
21432305

21442306
/** Whether to stream the response via SSE */

stagehand-java-core/src/test/kotlin/com/browserbase/api/models/sessions/SessionExecuteParamsTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ internal class SessionExecuteParamsTest {
5757
.maxSteps(20.0)
5858
.toolTimeout(30000.0)
5959
.useSearch(true)
60+
.variables(
61+
SessionExecuteParams.ExecuteOptions.Variables.builder()
62+
.putAdditionalProperty("foo", JsonValue.from("string"))
63+
.build()
64+
)
6065
.build()
6166
)
6267
.frameId("frameId")
@@ -133,6 +138,11 @@ internal class SessionExecuteParamsTest {
133138
.maxSteps(20.0)
134139
.toolTimeout(30000.0)
135140
.useSearch(true)
141+
.variables(
142+
SessionExecuteParams.ExecuteOptions.Variables.builder()
143+
.putAdditionalProperty("foo", JsonValue.from("string"))
144+
.build()
145+
)
136146
.build()
137147
)
138148
.frameId("frameId")
@@ -213,6 +223,11 @@ internal class SessionExecuteParamsTest {
213223
.maxSteps(20.0)
214224
.toolTimeout(30000.0)
215225
.useSearch(true)
226+
.variables(
227+
SessionExecuteParams.ExecuteOptions.Variables.builder()
228+
.putAdditionalProperty("foo", JsonValue.from("string"))
229+
.build()
230+
)
216231
.build()
217232
)
218233
.frameId("frameId")
@@ -266,6 +281,11 @@ internal class SessionExecuteParamsTest {
266281
.maxSteps(20.0)
267282
.toolTimeout(30000.0)
268283
.useSearch(true)
284+
.variables(
285+
SessionExecuteParams.ExecuteOptions.Variables.builder()
286+
.putAdditionalProperty("foo", JsonValue.from("string"))
287+
.build()
288+
)
269289
.build()
270290
)
271291
assertThat(body.frameId()).contains("frameId")

stagehand-java-core/src/test/kotlin/com/browserbase/api/services/async/SessionServiceAsyncTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ internal class SessionServiceAsyncTest {
215215
.maxSteps(20.0)
216216
.toolTimeout(30000.0)
217217
.useSearch(true)
218+
.variables(
219+
SessionExecuteParams.ExecuteOptions.Variables.builder()
220+
.putAdditionalProperty("foo", JsonValue.from("string"))
221+
.build()
222+
)
218223
.build()
219224
)
220225
.frameId("frameId")
@@ -285,6 +290,11 @@ internal class SessionServiceAsyncTest {
285290
.maxSteps(20.0)
286291
.toolTimeout(30000.0)
287292
.useSearch(true)
293+
.variables(
294+
SessionExecuteParams.ExecuteOptions.Variables.builder()
295+
.putAdditionalProperty("foo", JsonValue.from("string"))
296+
.build()
297+
)
288298
.build()
289299
)
290300
.frameId("frameId")

stagehand-java-core/src/test/kotlin/com/browserbase/api/services/blocking/SessionServiceTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ internal class SessionServiceTest {
213213
.maxSteps(20.0)
214214
.toolTimeout(30000.0)
215215
.useSearch(true)
216+
.variables(
217+
SessionExecuteParams.ExecuteOptions.Variables.builder()
218+
.putAdditionalProperty("foo", JsonValue.from("string"))
219+
.build()
220+
)
216221
.build()
217222
)
218223
.frameId("frameId")
@@ -282,6 +287,11 @@ internal class SessionServiceTest {
282287
.maxSteps(20.0)
283288
.toolTimeout(30000.0)
284289
.useSearch(true)
290+
.variables(
291+
SessionExecuteParams.ExecuteOptions.Variables.builder()
292+
.putAdditionalProperty("foo", JsonValue.from("string"))
293+
.build()
294+
)
285295
.build()
286296
)
287297
.frameId("frameId")

0 commit comments

Comments
 (0)