Skip to content

Commit fcdf0a1

Browse files
author
stlc-bot
committed
feat(scrape): add sourcePreference to choose between HTML conversion and published Markdown (#2010)
Stainless-Generated-From: 95d1d3475e3ebdd10577b3497d9eb2b71bdc3def
1 parent f90e57b commit fcdf0a1

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/charmbracelet/bubbletea v1.3.6
88
github.com/charmbracelet/lipgloss v1.1.0
99
github.com/charmbracelet/x/term v0.2.1
10-
github.com/context-dot-dev/context-go-sdk/v2 v2.31.3
10+
github.com/context-dot-dev/context-go-sdk/v2 v2.32.0
1111
github.com/goccy/go-yaml v1.18.0
1212
github.com/itchyny/json2yaml v0.1.4
1313
github.com/muesli/reflow v0.3.0

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payR
1818
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
1919
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
2020
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
21-
github.com/context-dot-dev/context-go-sdk/v2 v2.31.3 h1:GKW1xWJfGtqmjVidIWMLEszE5XOF7n7VWvKzxhup7kw=
22-
github.com/context-dot-dev/context-go-sdk/v2 v2.31.3/go.mod h1:CvRANWLP0FHj5XyUwzEVX79ujdwr/2Jy375UVzKNQm8=
21+
github.com/context-dot-dev/context-go-sdk/v2 v2.32.0 h1:rF7kVjfdI4QsHyRuZySwllnUpdrlWqMgvk6bdR1vk0k=
22+
github.com/context-dot-dev/context-go-sdk/v2 v2.32.0/go.mod h1:CvRANWLP0FHj5XyUwzEVX79ujdwr/2Jy375UVzKNQm8=
2323
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
2424
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2525
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

‎pkg/cmd/batch_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestBatchSubmit(t *testing.T) {
8484
t,
8585
"--api-key", "string",
8686
"batch", "submit",
87-
"--input", "{data: {formats: {bytes: true, highlights: true, html: true, images: true, json: true, markdown: true, parse: true, product: true, screenshot: true}, urls: [{url: https://example.com/products/anvil, itemId: sku-1, meta: {category: bar}}, {url: https://example.com/products/hammer, itemId: sku-2, meta: {foo: bar}}], highlightsParams: {query: x, maxCharacters: 100}, imageParams: {dedupe: none, enrich: [dimensions]}, jsonParams: {schema: {type: bar, properties: bar, required: bar, additionalProperties: bar}, instructions: instructions}, markdownParams: {includeImages: true, includeLinks: true, inlineImages: placeholder}, maxAgeMs: 0, parseParams: {rules: {title: h1, links: {selector: a, output: '@href', type: list}}, type: custom}, productParams: {dedupeImages: true}, screenshotParams: {area: viewport, format: png}, sharedParams: {actions: [{action: Click the product details tab, type: perform}], country: US, dismissCookies: true, dismissPopups: true, excludeSelectors: [P], headers: {Accept-Language: en-US}, includeFrames: true, includeSelectors: [P], mainContentOnly: true, parsers: {pdf: {endPage: 1, ocr: 'off', startPage: 1}}, profile: {profileId: prof_210b9798eb53baa4e69d31c1071cf03d, type: existing}, settleAnimations: true, theme: light, viewport: {height: 240, width: 240}, waitFor: 500}, tags: [production, team-alpha], timeoutOpts: {milliseconds: 1, behavior: fail}, zdr: disabled}, mode: scrape}",
87+
"--input", "{data: {formats: {bytes: true, highlights: true, html: true, images: true, json: true, markdown: true, parse: true, product: true, screenshot: true}, urls: [{url: https://example.com/products/anvil, itemId: sku-1, meta: {category: bar}}, {url: https://example.com/products/hammer, itemId: sku-2, meta: {foo: bar}}], highlightsParams: {query: x, maxCharacters: 100}, imageParams: {dedupe: none, enrich: [dimensions]}, jsonParams: {schema: {type: bar, properties: bar, required: bar, additionalProperties: bar}, instructions: instructions}, markdownParams: {includeImages: true, includeLinks: true, inlineImages: placeholder, sourcePreference: [html-conversion]}, maxAgeMs: 0, parseParams: {rules: {title: h1, links: {selector: a, output: '@href', type: list}}, type: custom}, productParams: {dedupeImages: true}, screenshotParams: {area: viewport, format: png}, sharedParams: {actions: [{action: Click the product details tab, type: perform}], country: US, dismissCookies: true, dismissPopups: true, excludeSelectors: [P], headers: {Accept-Language: en-US}, includeFrames: true, includeSelectors: [P], mainContentOnly: true, parsers: {pdf: {endPage: 1, ocr: 'off', startPage: 1}}, profile: {profileId: prof_210b9798eb53baa4e69d31c1071cf03d, type: existing}, settleAnimations: true, theme: light, viewport: {height: 240, width: 240}, waitFor: 500}, tags: [production, team-alpha], timeoutOpts: {milliseconds: 1, behavior: fail}, zdr: disabled}, mode: scrape}",
8888
"--tag", "docs",
8989
"--tag", "competitor",
9090
"--webhook", "{url: https://example.com, retry: {delays_seconds: [10, 60, 300, 1800, 7200, 21600, 57600]}}",
@@ -102,7 +102,7 @@ func TestBatchSubmit(t *testing.T) {
102102
t,
103103
"--api-key", "string",
104104
"batch", "submit",
105-
"--input", "{data: {formats: {bytes: true, highlights: true, html: true, images: true, json: true, markdown: true, parse: true, product: true, screenshot: true}, urls: [{url: https://example.com/products/anvil, itemId: sku-1, meta: {category: bar}}, {url: https://example.com/products/hammer, itemId: sku-2, meta: {foo: bar}}], highlightsParams: {query: x, maxCharacters: 100}, imageParams: {dedupe: none, enrich: [dimensions]}, jsonParams: {schema: {type: bar, properties: bar, required: bar, additionalProperties: bar}, instructions: instructions}, markdownParams: {includeImages: true, includeLinks: true, inlineImages: placeholder}, maxAgeMs: 0, parseParams: {rules: {title: h1, links: {selector: a, output: '@href', type: list}}, type: custom}, productParams: {dedupeImages: true}, screenshotParams: {area: viewport, format: png}, sharedParams: {actions: [{action: Click the product details tab, type: perform}], country: US, dismissCookies: true, dismissPopups: true, excludeSelectors: [P], headers: {Accept-Language: en-US}, includeFrames: true, includeSelectors: [P], mainContentOnly: true, parsers: {pdf: {endPage: 1, ocr: 'off', startPage: 1}}, profile: {profileId: prof_210b9798eb53baa4e69d31c1071cf03d, type: existing}, settleAnimations: true, theme: light, viewport: {height: 240, width: 240}, waitFor: 500}, tags: [production, team-alpha], timeoutOpts: {milliseconds: 1, behavior: fail}, zdr: disabled}, mode: scrape}",
105+
"--input", "{data: {formats: {bytes: true, highlights: true, html: true, images: true, json: true, markdown: true, parse: true, product: true, screenshot: true}, urls: [{url: https://example.com/products/anvil, itemId: sku-1, meta: {category: bar}}, {url: https://example.com/products/hammer, itemId: sku-2, meta: {foo: bar}}], highlightsParams: {query: x, maxCharacters: 100}, imageParams: {dedupe: none, enrich: [dimensions]}, jsonParams: {schema: {type: bar, properties: bar, required: bar, additionalProperties: bar}, instructions: instructions}, markdownParams: {includeImages: true, includeLinks: true, inlineImages: placeholder, sourcePreference: [html-conversion]}, maxAgeMs: 0, parseParams: {rules: {title: h1, links: {selector: a, output: '@href', type: list}}, type: custom}, productParams: {dedupeImages: true}, screenshotParams: {area: viewport, format: png}, sharedParams: {actions: [{action: Click the product details tab, type: perform}], country: US, dismissCookies: true, dismissPopups: true, excludeSelectors: [P], headers: {Accept-Language: en-US}, includeFrames: true, includeSelectors: [P], mainContentOnly: true, parsers: {pdf: {endPage: 1, ocr: 'off', startPage: 1}}, profile: {profileId: prof_210b9798eb53baa4e69d31c1071cf03d, type: existing}, settleAnimations: true, theme: light, viewport: {height: 240, width: 240}, waitFor: 500}, tags: [production, team-alpha], timeoutOpts: {milliseconds: 1, behavior: fail}, zdr: disabled}, mode: scrape}",
106106
"--tag", "docs",
107107
"--tag", "competitor",
108108
"--webhook.url", "https://example.com",
@@ -154,6 +154,8 @@ func TestBatchSubmit(t *testing.T) {
154154
" includeImages: true\n" +
155155
" includeLinks: true\n" +
156156
" inlineImages: placeholder\n" +
157+
" sourcePreference:\n" +
158+
" - html-conversion\n" +
157159
" maxAgeMs: 0\n" +
158160
" parseParams:\n" +
159161
" rules:\n" +

‎pkg/cmd/web.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ var webWebCrawlMd = requestflag.WithInnerFlags(cli.Command{
502502
},
503503
&requestflag.Flag[bool]{
504504
Name: "include-images",
505-
Usage: "Include image references in the Markdown output",
505+
Usage: "Include image references in the Markdown output Only applies to `html-conversion` extractions.",
506506
Default: false,
507507
BodyPath: "includeImages",
508508
},
509509
&requestflag.Flag[bool]{
510510
Name: "include-links",
511-
Usage: "Preserve hyperlinks in the Markdown output",
511+
Usage: "Preserve hyperlinks in the Markdown output Only applies to `html-conversion` extractions.",
512512
Default: true,
513513
BodyPath: "includeLinks",
514514
},
@@ -548,10 +548,16 @@ var webWebCrawlMd = requestflag.WithInnerFlags(cli.Command{
548548
},
549549
&requestflag.Flag[bool]{
550550
Name: "shorten-base64-images",
551-
Usage: "Truncate base64-encoded image data in the Markdown output",
551+
Usage: "Truncate base64-encoded image data in the Markdown output Only applies to `html-conversion` extractions.",
552552
Default: true,
553553
BodyPath: "shortenBase64Images",
554554
},
555+
&requestflag.Flag[[]string]{
556+
Name: "source-preference",
557+
Usage: "Where the Markdown should come from, in order of preference; the first usable source wins. `html-conversion` converts the page HTML, and counts as unusable when the conversion has almost no content (under 200 characters). `markdown-direct` uses the official Markdown version the page publishes through, returned as the site serves it. Defaults to `[\"html-conversion\", \"markdown-direct\"]`. With only `markdown-direct`, a page that publishes no Markdown fails with `NO_PUBLISHED_MARKDOWN` and is not billed. Requests with include or exclude selectors never use `markdown-direct`, so they must include `html-conversion`.",
558+
Default: []string{"html-conversion", "markdown-direct"},
559+
BodyPath: "sourcePreference",
560+
},
555561
&requestflag.Flag[int64]{
556562
Name: "stop-after-ms",
557563
Usage: "Soft crawl deadline in milliseconds. Returns pages collected before the next deadline check.",

‎pkg/cmd/web_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ func TestWebWebCrawlMd(t *testing.T) {
337337
"--pdf", "{end: 1, ocr: true, shouldParse: true, start: 1}",
338338
"--settle-animations=true",
339339
"--shorten-base64-images=true",
340+
"--source-preference", "html-conversion",
340341
"--stop-after-ms", "10000",
341342
"--tag", "production",
342343
"--tag", "team-alpha",
@@ -375,6 +376,7 @@ func TestWebWebCrawlMd(t *testing.T) {
375376
"--pdf.start", "1",
376377
"--settle-animations=true",
377378
"--shorten-base64-images=true",
379+
"--source-preference", "html-conversion",
378380
"--stop-after-ms", "10000",
379381
"--tag", "production",
380382
"--tag", "team-alpha",
@@ -411,6 +413,8 @@ func TestWebWebCrawlMd(t *testing.T) {
411413
" start: 1\n" +
412414
"settleAnimations: true\n" +
413415
"shortenBase64Images: true\n" +
416+
"sourcePreference:\n" +
417+
" - html-conversion\n" +
414418
"stopAfterMs: 10000\n" +
415419
"tags:\n" +
416420
" - production\n" +

0 commit comments

Comments
 (0)