Skip to content

Commit 89485cb

Browse files
author
stlc-bot
committed
feat(scrape): add highlights format to POST /web/scrape (#1210)
Stainless-Generated-From: 3aeff5b07cbc2ffae72b3f43c05f4268a8d14650
1 parent babaf1d commit 89485cb

10 files changed

Lines changed: 269 additions & 8 deletions

File tree

‎src/ServiceContracts/WebContract.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ContextDev\Web\WebExtractStyleguideResponse;
1616
use ContextDev\Web\WebMapURLsResponse;
1717
use ContextDev\Web\WebScrapeParams\Formats;
18+
use ContextDev\Web\WebScrapeParams\HighlightsParams;
1819
use ContextDev\Web\WebScrapeParams\ImageParams;
1920
use ContextDev\Web\WebScrapeParams\JsonParams;
2021
use ContextDev\Web\WebScrapeParams\MarkdownParams;
@@ -39,6 +40,7 @@
3940
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebExtractStyleguideParams\TimeoutOpts as TimeoutOptsShape2
4041
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebMapURLsParams\TimeoutOpts as TimeoutOptsShape3
4142
* @phpstan-import-type FormatsShape from \ContextDev\Web\WebScrapeParams\Formats
43+
* @phpstan-import-type HighlightsParamsShape from \ContextDev\Web\WebScrapeParams\HighlightsParams
4244
* @phpstan-import-type ImageParamsShape from \ContextDev\Web\WebScrapeParams\ImageParams
4345
* @phpstan-import-type JsonParamsShape from \ContextDev\Web\WebScrapeParams\JsonParams
4446
* @phpstan-import-type MarkdownParamsShape from \ContextDev\Web\WebScrapeParams\MarkdownParams
@@ -161,6 +163,7 @@ public function mapUrls(
161163
*
162164
* @param Formats|FormatsShape $formats Outputs to return. Enable at least one; omitted formats are false.
163165
* @param string $url the URL to scrape
166+
* @param HighlightsParams|HighlightsParamsShape $highlightsParams Highlight options. Requires formats.highlights: true.
164167
* @param ImageParams|ImageParamsShape $imageParams Image options. Requires formats.images: true.
165168
* @param JsonParams|JsonParamsShape $jsonParams Required when formats.json is true.
166169
* @param MarkdownParams|MarkdownParamsShape $markdownParams Markdown options. Requires formats.markdown: true.
@@ -170,14 +173,15 @@ public function mapUrls(
170173
* @param SharedParams|SharedParamsShape $sharedParams Shared browser and content settings. Content filters leave screenshots and original bytes unchanged.
171174
* @param list<string> $tags Labels for tracking request usage. Not retained when zdr is enabled.
172175
* @param \ContextDev\Web\WebScrapeParams\TimeoutOpts|TimeoutOptsShape4 $timeoutOpts Total deadline, including navigation, actions, waiting, and all outputs. Defaults to 60000 milliseconds with behavior fail. Use return-partial to capture the current page state and return captured images if image processing cannot finish before the deadline; these responses set isPartial and are not cached. Every requested format must still be available. Fixed waits must fit before a response reserve of up to 5000 milliseconds (at most one quarter of the timeout) when using return-partial.
173-
* @param \ContextDev\Web\WebScrapeParams\Zdr|value-of<\ContextDev\Web\WebScrapeParams\Zdr> $zdr Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization.
176+
* @param \ContextDev\Web\WebScrapeParams\Zdr|value-of<\ContextDev\Web\WebScrapeParams\Zdr> $zdr Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization. Not available with the highlights output.
174177
* @param RequestOpts|null $requestOptions
175178
*
176179
* @throws APIException
177180
*/
178181
public function scrape(
179182
Formats|array $formats,
180183
string $url,
184+
HighlightsParams|array|null $highlightsParams = null,
181185
ImageParams|array|null $imageParams = null,
182186
JsonParams|array|null $jsonParams = null,
183187
MarkdownParams|array|null $markdownParams = null,

‎src/Services/WebRawService.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ContextDev\Web\WebMapURLsResponse;
2525
use ContextDev\Web\WebScrapeParams;
2626
use ContextDev\Web\WebScrapeParams\Formats;
27+
use ContextDev\Web\WebScrapeParams\HighlightsParams;
2728
use ContextDev\Web\WebScrapeParams\ImageParams;
2829
use ContextDev\Web\WebScrapeParams\JsonParams;
2930
use ContextDev\Web\WebScrapeParams\MarkdownParams;
@@ -51,6 +52,7 @@
5152
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebExtractStyleguideParams\TimeoutOpts as TimeoutOptsShape2
5253
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebMapURLsParams\TimeoutOpts as TimeoutOptsShape3
5354
* @phpstan-import-type FormatsShape from \ContextDev\Web\WebScrapeParams\Formats
55+
* @phpstan-import-type HighlightsParamsShape from \ContextDev\Web\WebScrapeParams\HighlightsParams
5456
* @phpstan-import-type ImageParamsShape from \ContextDev\Web\WebScrapeParams\ImageParams
5557
* @phpstan-import-type JsonParamsShape from \ContextDev\Web\WebScrapeParams\JsonParams
5658
* @phpstan-import-type MarkdownParamsShape from \ContextDev\Web\WebScrapeParams\MarkdownParams
@@ -236,11 +238,12 @@ public function mapUrls(
236238
/**
237239
* @api
238240
*
239-
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. One credit per request, including cache hits and missing pages, or two with browser actions; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
241+
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, highlights, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. Highlights return the plain-text passages most relevant to highlightsParams.query. One credit per request, including cache hits and missing pages, or two with browser actions; highlights add 3 credits when passages are returned; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
240242
*
241243
* @param array{
242244
* formats: Formats|FormatsShape,
243245
* url: string,
246+
* highlightsParams?: HighlightsParams|HighlightsParamsShape,
244247
* imageParams?: ImageParams|ImageParamsShape,
245248
* jsonParams?: JsonParams|JsonParamsShape,
246249
* markdownParams?: MarkdownParams|MarkdownParamsShape,

‎src/Services/WebService.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ContextDev\Web\WebExtractStyleguideResponse;
1919
use ContextDev\Web\WebMapURLsResponse;
2020
use ContextDev\Web\WebScrapeParams\Formats;
21+
use ContextDev\Web\WebScrapeParams\HighlightsParams;
2122
use ContextDev\Web\WebScrapeParams\ImageParams;
2223
use ContextDev\Web\WebScrapeParams\JsonParams;
2324
use ContextDev\Web\WebScrapeParams\MarkdownParams;
@@ -42,6 +43,7 @@
4243
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebExtractStyleguideParams\TimeoutOpts as TimeoutOptsShape2
4344
* @phpstan-import-type TimeoutOptsShape from \ContextDev\Web\WebMapURLsParams\TimeoutOpts as TimeoutOptsShape3
4445
* @phpstan-import-type FormatsShape from \ContextDev\Web\WebScrapeParams\Formats
46+
* @phpstan-import-type HighlightsParamsShape from \ContextDev\Web\WebScrapeParams\HighlightsParams
4547
* @phpstan-import-type ImageParamsShape from \ContextDev\Web\WebScrapeParams\ImageParams
4648
* @phpstan-import-type JsonParamsShape from \ContextDev\Web\WebScrapeParams\JsonParams
4749
* @phpstan-import-type MarkdownParamsShape from \ContextDev\Web\WebScrapeParams\MarkdownParams
@@ -251,10 +253,11 @@ public function mapUrls(
251253
/**
252254
* @api
253255
*
254-
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. One credit per request, including cache hits and missing pages, or two with browser actions; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
256+
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, highlights, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. Highlights return the plain-text passages most relevant to highlightsParams.query. One credit per request, including cache hits and missing pages, or two with browser actions; highlights add 3 credits when passages are returned; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
255257
*
256258
* @param Formats|FormatsShape $formats Outputs to return. Enable at least one; omitted formats are false.
257259
* @param string $url the URL to scrape
260+
* @param HighlightsParams|HighlightsParamsShape $highlightsParams Highlight options. Requires formats.highlights: true.
258261
* @param ImageParams|ImageParamsShape $imageParams Image options. Requires formats.images: true.
259262
* @param JsonParams|JsonParamsShape $jsonParams Required when formats.json is true.
260263
* @param MarkdownParams|MarkdownParamsShape $markdownParams Markdown options. Requires formats.markdown: true.
@@ -264,14 +267,15 @@ public function mapUrls(
264267
* @param SharedParams|SharedParamsShape $sharedParams Shared browser and content settings. Content filters leave screenshots and original bytes unchanged.
265268
* @param list<string> $tags Labels for tracking request usage. Not retained when zdr is enabled.
266269
* @param \ContextDev\Web\WebScrapeParams\TimeoutOpts|TimeoutOptsShape4 $timeoutOpts Total deadline, including navigation, actions, waiting, and all outputs. Defaults to 60000 milliseconds with behavior fail. Use return-partial to capture the current page state and return captured images if image processing cannot finish before the deadline; these responses set isPartial and are not cached. Every requested format must still be available. Fixed waits must fit before a response reserve of up to 5000 milliseconds (at most one quarter of the timeout) when using return-partial.
267-
* @param \ContextDev\Web\WebScrapeParams\Zdr|value-of<\ContextDev\Web\WebScrapeParams\Zdr> $zdr Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization.
270+
* @param \ContextDev\Web\WebScrapeParams\Zdr|value-of<\ContextDev\Web\WebScrapeParams\Zdr> $zdr Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization. Not available with the highlights output.
268271
* @param RequestOpts|null $requestOptions
269272
*
270273
* @throws APIException
271274
*/
272275
public function scrape(
273276
Formats|array $formats,
274277
string $url,
278+
HighlightsParams|array|null $highlightsParams = null,
275279
ImageParams|array|null $imageParams = null,
276280
JsonParams|array|null $jsonParams = null,
277281
MarkdownParams|array|null $markdownParams = null,
@@ -290,6 +294,7 @@ public function scrape(
290294
[
291295
'formats' => $formats,
292296
'url' => $url,
297+
'highlightsParams' => $highlightsParams,
293298
'imageParams' => $imageParams,
294299
'jsonParams' => $jsonParams,
295300
'markdownParams' => $markdownParams,

‎src/Web/WebScrapeParams.php‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ContextDev\Core\Concerns\SdkParams;
1111
use ContextDev\Core\Contracts\BaseModel;
1212
use ContextDev\Web\WebScrapeParams\Formats;
13+
use ContextDev\Web\WebScrapeParams\HighlightsParams;
1314
use ContextDev\Web\WebScrapeParams\ImageParams;
1415
use ContextDev\Web\WebScrapeParams\JsonParams;
1516
use ContextDev\Web\WebScrapeParams\MarkdownParams;
@@ -20,11 +21,12 @@
2021
use ContextDev\Web\WebScrapeParams\Zdr;
2122

2223
/**
23-
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. One credit per request, including cache hits and missing pages, or two with browser actions; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
24+
* Reuse cached outputs independently and capture missing formats in one page visit. Each cache key includes only the settings that affect that output. HTML is shared with Markdown, parsed fields, highlights, and JSON extraction. Cached outputs can come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests use the existing fast acquisition path. Highlights return the plain-text passages most relevant to highlightsParams.query. One credit per request, including cache hits and missing pages, or two with browser actions; highlights add 3 credits when passages are returned; JSON extraction adds four credits and runs an LLM over the page Markdown on every request that has text to extract; PDF OCR adds one credit per recovered page on fresh extraction. Original response bytes and screenshots are limited to 20 MiB each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
2425
*
2526
* @see ContextDev\Services\WebService::scrape()
2627
*
2728
* @phpstan-import-type FormatsShape from \ContextDev\Web\WebScrapeParams\Formats
29+
* @phpstan-import-type HighlightsParamsShape from \ContextDev\Web\WebScrapeParams\HighlightsParams
2830
* @phpstan-import-type ImageParamsShape from \ContextDev\Web\WebScrapeParams\ImageParams
2931
* @phpstan-import-type JsonParamsShape from \ContextDev\Web\WebScrapeParams\JsonParams
3032
* @phpstan-import-type MarkdownParamsShape from \ContextDev\Web\WebScrapeParams\MarkdownParams
@@ -36,6 +38,7 @@
3638
* @phpstan-type WebScrapeParamsShape = array{
3739
* formats: Formats|FormatsShape,
3840
* url: string,
41+
* highlightsParams?: null|HighlightsParams|HighlightsParamsShape,
3942
* imageParams?: null|ImageParams|ImageParamsShape,
4043
* jsonParams?: null|JsonParams|JsonParamsShape,
4144
* markdownParams?: null|MarkdownParams|MarkdownParamsShape,
@@ -66,6 +69,12 @@ final class WebScrapeParams implements BaseModel
6669
#[Required]
6770
public string $url;
6871

72+
/**
73+
* Highlight options. Requires formats.highlights: true.
74+
*/
75+
#[Optional]
76+
public ?HighlightsParams $highlightsParams;
77+
6978
/**
7079
* Image options. Requires formats.images: true.
7180
*/
@@ -123,7 +132,7 @@ final class WebScrapeParams implements BaseModel
123132
public ?TimeoutOpts $timeoutOpts;
124133

125134
/**
126-
* Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization.
135+
* Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization. Not available with the highlights output.
127136
*
128137
* @var value-of<Zdr>|null $zdr
129138
*/
@@ -155,6 +164,7 @@ public function __construct()
155164
* You must use named parameters to construct any parameters with a default value.
156165
*
157166
* @param Formats|FormatsShape $formats
167+
* @param HighlightsParams|HighlightsParamsShape|null $highlightsParams
158168
* @param ImageParams|ImageParamsShape|null $imageParams
159169
* @param JsonParams|JsonParamsShape|null $jsonParams
160170
* @param MarkdownParams|MarkdownParamsShape|null $markdownParams
@@ -168,6 +178,7 @@ public function __construct()
168178
public static function with(
169179
Formats|array $formats,
170180
string $url,
181+
HighlightsParams|array|null $highlightsParams = null,
171182
ImageParams|array|null $imageParams = null,
172183
JsonParams|array|null $jsonParams = null,
173184
MarkdownParams|array|null $markdownParams = null,
@@ -184,6 +195,7 @@ public static function with(
184195
$self['formats'] = $formats;
185196
$self['url'] = $url;
186197

198+
null !== $highlightsParams && $self['highlightsParams'] = $highlightsParams;
187199
null !== $imageParams && $self['imageParams'] = $imageParams;
188200
null !== $jsonParams && $self['jsonParams'] = $jsonParams;
189201
null !== $markdownParams && $self['markdownParams'] = $markdownParams;
@@ -222,6 +234,20 @@ public function withURL(string $url): self
222234
return $self;
223235
}
224236

237+
/**
238+
* Highlight options. Requires formats.highlights: true.
239+
*
240+
* @param HighlightsParams|HighlightsParamsShape $highlightsParams
241+
*/
242+
public function withHighlightsParams(
243+
HighlightsParams|array $highlightsParams
244+
): self {
245+
$self = clone $this;
246+
$self['highlightsParams'] = $highlightsParams;
247+
248+
return $self;
249+
}
250+
225251
/**
226252
* Image options. Requires formats.images: true.
227253
*
@@ -340,7 +366,7 @@ public function withTimeoutOpts(TimeoutOpts|array $timeoutOpts): self
340366
}
341367

342368
/**
343-
* Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization.
369+
* Zero data retention. Bypasses caches and uploads; excludes request/response content and tags from logs. Must be enabled for your organization. Not available with the highlights output.
344370
*
345371
* @param Zdr|value-of<Zdr> $zdr
346372
*/

‎src/Web/WebScrapeParams/Formats.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*
1414
* @phpstan-type FormatsShape = array{
1515
* bytes?: bool|null,
16+
* highlights?: bool|null,
1617
* html?: bool|null,
1718
* images?: bool|null,
1819
* json?: bool|null,
@@ -32,6 +33,12 @@ final class Formats implements BaseModel
3233
#[Optional]
3334
public ?bool $bytes;
3435

36+
/**
37+
* Plain-text passages from the page that are most relevant to highlightsParams.query, each prefixed with its section heading. Adds 3 credits. Not available with zdr enabled.
38+
*/
39+
#[Optional]
40+
public ?bool $highlights;
41+
3542
/**
3643
* Rendered HTML.
3744
*/
@@ -80,6 +87,7 @@ public function __construct()
8087
*/
8188
public static function with(
8289
?bool $bytes = null,
90+
?bool $highlights = null,
8391
?bool $html = null,
8492
?bool $images = null,
8593
?bool $json = null,
@@ -90,6 +98,7 @@ public static function with(
9098
$self = new self;
9199

92100
null !== $bytes && $self['bytes'] = $bytes;
101+
null !== $highlights && $self['highlights'] = $highlights;
93102
null !== $html && $self['html'] = $html;
94103
null !== $images && $self['images'] = $images;
95104
null !== $json && $self['json'] = $json;
@@ -111,6 +120,17 @@ public function withBytes(bool $bytes): self
111120
return $self;
112121
}
113122

123+
/**
124+
* Plain-text passages from the page that are most relevant to highlightsParams.query, each prefixed with its section heading. Adds 3 credits. Not available with zdr enabled.
125+
*/
126+
public function withHighlights(bool $highlights): self
127+
{
128+
$self = clone $this;
129+
$self['highlights'] = $highlights;
130+
131+
return $self;
132+
}
133+
114134
/**
115135
* Rendered HTML.
116136
*/

0 commit comments

Comments
 (0)