Skip to content

Commit 043d138

Browse files
feat: Add screenshot option to Extract
1 parent f072067 commit 043d138

5 files changed

Lines changed: 63 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-e77d6b15f0a94b16a54ef87a84d2cabe49eb11cff5ceba76f00dd788ff483eab.yml
3-
openapi_spec_hash: a1dab7fe72a772d188a15305124ebd73
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-80502d74c1be605e77d45ff2b54297fe34ce85dbad1e8f2dfa30ba6d09601219.yml
3+
openapi_spec_hash: fd62f768756a400c3ecd695bfcf3845a
44
config_hash: 1fb12ae9b478488bc1e56bfbdc210b01

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ private constructor(
635635
private constructor(
636636
private val ignoreSelectors: JsonField<List<String>>,
637637
private val model: JsonField<Model>,
638+
private val screenshot: JsonField<Boolean>,
638639
private val selector: JsonField<String>,
639640
private val timeout: JsonField<Double>,
640641
private val additionalProperties: MutableMap<String, JsonValue>,
@@ -646,11 +647,14 @@ private constructor(
646647
@ExcludeMissing
647648
ignoreSelectors: JsonField<List<String>> = JsonMissing.of(),
648649
@JsonProperty("model") @ExcludeMissing model: JsonField<Model> = JsonMissing.of(),
650+
@JsonProperty("screenshot")
651+
@ExcludeMissing
652+
screenshot: JsonField<Boolean> = JsonMissing.of(),
649653
@JsonProperty("selector")
650654
@ExcludeMissing
651655
selector: JsonField<String> = JsonMissing.of(),
652656
@JsonProperty("timeout") @ExcludeMissing timeout: JsonField<Double> = JsonMissing.of(),
653-
) : this(ignoreSelectors, model, selector, timeout, mutableMapOf())
657+
) : this(ignoreSelectors, model, screenshot, selector, timeout, mutableMapOf())
654658

655659
/**
656660
* Selectors for elements and subtrees that should be excluded from extraction
@@ -669,6 +673,15 @@ private constructor(
669673
*/
670674
fun model(): Optional<Model> = model.getOptional("model")
671675

676+
/**
677+
* When true, include a screenshot of the current viewport in the extraction LLM call.
678+
* Defaults to false.
679+
*
680+
* @throws StagehandInvalidDataException if the JSON field has an unexpected type (e.g. if
681+
* the server responded with an unexpected value).
682+
*/
683+
fun screenshot(): Optional<Boolean> = screenshot.getOptional("screenshot")
684+
672685
/**
673686
* CSS selector to scope extraction to a specific element
674687
*
@@ -702,6 +715,15 @@ private constructor(
702715
*/
703716
@JsonProperty("model") @ExcludeMissing fun _model(): JsonField<Model> = model
704717

718+
/**
719+
* Returns the raw JSON value of [screenshot].
720+
*
721+
* Unlike [screenshot], this method doesn't throw if the JSON field has an unexpected type.
722+
*/
723+
@JsonProperty("screenshot")
724+
@ExcludeMissing
725+
fun _screenshot(): JsonField<Boolean> = screenshot
726+
705727
/**
706728
* Returns the raw JSON value of [selector].
707729
*
@@ -739,6 +761,7 @@ private constructor(
739761

740762
private var ignoreSelectors: JsonField<MutableList<String>>? = null
741763
private var model: JsonField<Model> = JsonMissing.of()
764+
private var screenshot: JsonField<Boolean> = JsonMissing.of()
742765
private var selector: JsonField<String> = JsonMissing.of()
743766
private var timeout: JsonField<Double> = JsonMissing.of()
744767
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
@@ -747,6 +770,7 @@ private constructor(
747770
internal fun from(options: Options) = apply {
748771
ignoreSelectors = options.ignoreSelectors.map { it.toMutableList() }
749772
model = options.model
773+
screenshot = options.screenshot
750774
selector = options.selector
751775
timeout = options.timeout
752776
additionalProperties = options.additionalProperties.toMutableMap()
@@ -797,6 +821,21 @@ private constructor(
797821
/** Alias for calling [model] with `Model.ofString(string)`. */
798822
fun model(string: String) = model(Model.ofString(string))
799823

824+
/**
825+
* When true, include a screenshot of the current viewport in the extraction LLM call.
826+
* Defaults to false.
827+
*/
828+
fun screenshot(screenshot: Boolean) = screenshot(JsonField.of(screenshot))
829+
830+
/**
831+
* Sets [Builder.screenshot] to an arbitrary JSON value.
832+
*
833+
* You should usually call [Builder.screenshot] with a well-typed [Boolean] value
834+
* instead. This method is primarily for setting the field to an undocumented or not yet
835+
* supported value.
836+
*/
837+
fun screenshot(screenshot: JsonField<Boolean>) = apply { this.screenshot = screenshot }
838+
800839
/** CSS selector to scope extraction to a specific element */
801840
fun selector(selector: String) = selector(JsonField.of(selector))
802841

@@ -849,6 +888,7 @@ private constructor(
849888
Options(
850889
(ignoreSelectors ?: JsonMissing.of()).map { it.toImmutable() },
851890
model,
891+
screenshot,
852892
selector,
853893
timeout,
854894
additionalProperties.toMutableMap(),
@@ -873,6 +913,7 @@ private constructor(
873913

874914
ignoreSelectors()
875915
model().ifPresent { it.validate() }
916+
screenshot()
876917
selector()
877918
timeout()
878919
validated = true
@@ -896,6 +937,7 @@ private constructor(
896937
internal fun validity(): Int =
897938
(ignoreSelectors.asKnown().getOrNull()?.size ?: 0) +
898939
(model.asKnown().getOrNull()?.validity() ?: 0) +
940+
(if (screenshot.asKnown().isPresent) 1 else 0) +
899941
(if (selector.asKnown().isPresent) 1 else 0) +
900942
(if (timeout.asKnown().isPresent) 1 else 0)
901943

@@ -1119,19 +1161,27 @@ private constructor(
11191161
return other is Options &&
11201162
ignoreSelectors == other.ignoreSelectors &&
11211163
model == other.model &&
1164+
screenshot == other.screenshot &&
11221165
selector == other.selector &&
11231166
timeout == other.timeout &&
11241167
additionalProperties == other.additionalProperties
11251168
}
11261169

11271170
private val hashCode: Int by lazy {
1128-
Objects.hash(ignoreSelectors, model, selector, timeout, additionalProperties)
1171+
Objects.hash(
1172+
ignoreSelectors,
1173+
model,
1174+
screenshot,
1175+
selector,
1176+
timeout,
1177+
additionalProperties,
1178+
)
11291179
}
11301180

11311181
override fun hashCode(): Int = hashCode
11321182

11331183
override fun toString() =
1134-
"Options{ignoreSelectors=$ignoreSelectors, model=$model, selector=$selector, timeout=$timeout, additionalProperties=$additionalProperties}"
1184+
"Options{ignoreSelectors=$ignoreSelectors, model=$model, screenshot=$screenshot, selector=$selector, timeout=$timeout, additionalProperties=$additionalProperties}"
11351185
}
11361186

11371187
/** JSON Schema defining the structure of data to extract */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ internal class SessionExtractParamsTest {
5959
.provider(ModelConfig.Provider.OPENAI)
6060
.build()
6161
)
62+
.screenshot(false)
6263
.selector("#main-content")
6364
.timeout(30000.0)
6465
.build()
@@ -132,6 +133,7 @@ internal class SessionExtractParamsTest {
132133
.provider(ModelConfig.Provider.OPENAI)
133134
.build()
134135
)
136+
.screenshot(false)
135137
.selector("#main-content")
136138
.timeout(30000.0)
137139
.build()
@@ -209,6 +211,7 @@ internal class SessionExtractParamsTest {
209211
.provider(ModelConfig.Provider.OPENAI)
210212
.build()
211213
)
214+
.screenshot(false)
212215
.selector("#main-content")
213216
.timeout(30000.0)
214217
.build()
@@ -269,6 +272,7 @@ internal class SessionExtractParamsTest {
269272
.provider(ModelConfig.Provider.OPENAI)
270273
.build()
271274
)
275+
.screenshot(false)
272276
.selector("#main-content")
273277
.timeout(30000.0)
274278
.build()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ internal class SessionServiceAsyncTest {
537537
.provider(ModelConfig.Provider.OPENAI)
538538
.build()
539539
)
540+
.screenshot(false)
540541
.selector("#main-content")
541542
.timeout(30000.0)
542543
.build()
@@ -615,6 +616,7 @@ internal class SessionServiceAsyncTest {
615616
.provider(ModelConfig.Provider.OPENAI)
616617
.build()
617618
)
619+
.screenshot(false)
618620
.selector("#main-content")
619621
.timeout(30000.0)
620622
.build()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ internal class SessionServiceTest {
534534
.provider(ModelConfig.Provider.OPENAI)
535535
.build()
536536
)
537+
.screenshot(false)
537538
.selector("#main-content")
538539
.timeout(30000.0)
539540
.build()
@@ -611,6 +612,7 @@ internal class SessionServiceTest {
611612
.provider(ModelConfig.Provider.OPENAI)
612613
.build()
613614
)
615+
.screenshot(false)
614616
.selector("#main-content")
615617
.timeout(30000.0)
616618
.build()

0 commit comments

Comments
 (0)