ask-web-fetch 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ad48d09aa8869b4193a703ad6a760753c2767c9e26a6919b1405a5fb186c7a4
4
- data.tar.gz: 52960016a3d4740f4913d4879cbf80edacf95e4ffc07601e6dde0d80515ffe81
3
+ metadata.gz: b2dd2d3ee4ae5d53f7e1bf441d3b598be5195d3aafd58119213b7b739ad900db
4
+ data.tar.gz: 49d8aa803e8f4871e278d6fbe7ad71b6a6826c5afe62c8dc8f38abc2b8b4cb8e
5
5
  SHA512:
6
- metadata.gz: fa6d778abc6918bb7ae839d4f9c96dafe215014a8b2f4efc8027ab1e49d287e3ca57204f54f013531e38dff7393ac23f6da9619b3afd48efae8b6f7e42dfa0d9
7
- data.tar.gz: 7d4e5fe0e70b28cc3349ec96d071d4e1cc7a46c2c21ca8f41c35968d8b15221854c2418fc72ae2a8e56e97c07930592b2ee5b79da921c4a4ccb4fe6990c7e938
6
+ metadata.gz: 95f40993df27de4c059f085ece2d71b5ea8c1e72c2d3d3dc42e96d25a1d5d87a400f5905cbaf6685675eb064ddebe95fa110609f16b93f586a5a31e02e17917c
7
+ data.tar.gz: a5b8513eef5cad48fbed247a2578e662c42a401b7b8e16028e5b6fbfa90d6b28da9e3db5786c20d43182ececf5a490ff98338a4fbf3ba286b0e4f9123210dea2
@@ -66,7 +66,14 @@ module Ask
66
66
  # challenge/interstitial pages carry these markers, while legitimate
67
67
  # pages can contain the word "captcha" in unrelated config/JS (e.g.
68
68
  # Wikipedia embeds an hcaptcha edit-config flag on every page).
69
- CHALLENGE_RE = /just a moment|checking your browser|cf-chl/i
69
+ # Cf-chl: a Turnstile form widget uses `cf-chl-widget-*` + `cf-turnstile-response`
70
+ # (a per-form CAPTCHA, not the `cf-chl` managed challenge that gates
71
+ # the whole page), so matching bare `cf-chl` on the body misclassifies
72
+ # every Turnstile form (openai.com/form/codex-for-oss) as a challenge.
73
+ # Accept both the classic managed-challenge markers (`challenge-platform`,
74
+ # `_cf_chl_opt`) and the bare `cf-chl` id, but the plain widget id is
75
+ # explicitly NOT a challenge — see challenge_page? below.
76
+ CHALLENGE_RE = /just a moment|checking your browser|cf-chl|challenge-platform|_cf_chl_opt/i
70
77
 
71
78
  # Registrar parking-page markers: the page is an ad for a parked
72
79
  # (for-sale) domain, not the site's content. A content company must
@@ -159,7 +166,17 @@ module Ask
159
166
  private
160
167
 
161
168
  def challenge_page?(body)
162
- body.to_s.match?(CHALLENGE_RE)
169
+ text = body.to_s
170
+ return false unless text.match?(CHALLENGE_RE)
171
+ # Bare `cf-chl-widget-*` is a Turnstile form widget, not the
172
+ # Cloudflare managed challenge interstitial. The interstitial's
173
+ # `cf-chl` comes with `challenge-platform` / `_cf_chl_opt` /
174
+ # "just a moment" next to it; a page whose only hit is the widget
175
+ # id (openai.com form pages) is NOT a challenge.
176
+ return false if text.include?('cf-chl-widget') &&
177
+ !text.match?(/challenge-platform|_cf_chl_opt|just a moment|checking your browser/i)
178
+
179
+ true
163
180
  end
164
181
 
165
182
  def parked_domain?(body)
@@ -38,7 +38,33 @@ module Ask
38
38
  end
39
39
  end
40
40
 
41
+ # Agent-first content negotiation: many sites now serve clean
42
+ # markdown when asked via Accept: text/markdown, a .md URL twin,
43
+ # or an /llms.txt manifest. We probe these low-cost paths before
44
+ # falling back to full HTML scrape + DOM conversion.
45
+ #
46
+ # Order: (1) Accept: text/markdown on the original URL — the
47
+ # cheapest probe, one extra GET; (2) the .md twin — Mintlify-
48
+ # style sites redirect .md with content-type text/plain; (3) the
49
+ # full HTML scrape. llms.txt manifests are upstream of individual
50
+ # pages (they index the site) and are tried by the MCP tool
51
+ # layer, not per-URL — that avoids duplicate fetches when the
52
+ # same manifest covers multiple URLs.
41
53
  def fetch(url)
54
+ # Probe 1: server content negotiation
55
+ md_body, md_ct, md_redirect = fetch_markdown(url)
56
+ if md_body && !md_body.empty?
57
+ return assemble_page(md_body, url, md_redirect, source: :accept_header)
58
+ end
59
+
60
+ # Probe 2: .md URL twin (Mintlify, Docusaurus, some Hugo sites)
61
+ twin = "#{url.chomp('/')}.md"
62
+ md_body, md_ct, md_redirect = fetch_markdown(twin)
63
+ if md_body && !md_body.empty?
64
+ return assemble_page(md_body, url, md_redirect, source: :md_twin, twin_url: twin)
65
+ end
66
+
67
+ # Probe 3: full HTML scrape (legacy path)
42
68
  body, content_type, redirect = fetch_html(url)
43
69
  raise FetchError, "expected HTML from #{url}, got #{content_type}" unless content_type.include?('html')
44
70
  raise FetchError, "challenge page at #{url}" if challenge_page?(body)
@@ -183,6 +209,60 @@ module Ask
183
209
  def redirect_info(status, uri)
184
210
  status && { status: status, url: uri.to_s }
185
211
  end
212
+
213
+ # Probes +url+ with Accept: text/markdown. Returns
214
+ # [body, content_type, redirect] on a text/markdown response, or
215
+ # nils when the server returned HTML (or anything else the caller
216
+ # shouldn't treat as agent-native). Follows one redirect hop —
217
+ # enough for Mintlify's 307 → .md twin.
218
+ def fetch_markdown(url)
219
+ uri = URI(url)
220
+ response = self.class.http.get(
221
+ uri.to_s,
222
+ headers: { 'accept' => 'text/markdown' }
223
+ )
224
+ return [nil, nil, nil] unless response
225
+ return [nil, nil, nil] if response.status >= 400
226
+
227
+ redirect_info = nil
228
+
229
+ # Follow a single redirect (Mintlify 307 → .md twin)
230
+ if (300..399).cover?(response.status) && !response.location.empty?
231
+ redirect_uri = URI.join(uri, response.location)
232
+ redirect_info = { status: response.status, url: redirect_uri.to_s }
233
+ response = self.class.http.get(
234
+ redirect_uri.to_s,
235
+ headers: { 'accept' => 'text/markdown' }
236
+ )
237
+ return [nil, nil, nil] unless response && response.status == 200
238
+ end
239
+
240
+ ct = response.content_type.to_s.downcase
241
+ return [nil, nil, nil] unless ct.include?('text/markdown') && response.status == 200
242
+
243
+ [response.body, ct, redirect_info]
244
+ rescue StandardError
245
+ [nil, nil, nil]
246
+ end
247
+
248
+ # Assembles a page hash from agent-native markdown (Accept or .md
249
+ # twin), skipping the HTML→markdown conversion pipeline. Runs the
250
+ # shared guards (parked domain, minimum content) so downstream
251
+ # behavior is identical regardless of source.
252
+ def assemble_page(markdown, url, redirect, source:, twin_url: nil)
253
+ source_url = twin_url || url
254
+ page = {
255
+ title: nil,
256
+ description: nil,
257
+ content: Markdown.clean(markdown),
258
+ redirected: redirect,
259
+ licenses: [],
260
+ outlinks: markdown_outlinks(markdown, source_url)
261
+ }
262
+ guard_page!(url, page[:content])
263
+
264
+ page
265
+ end
186
266
  end
187
267
  end
188
268
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ask
4
4
  module WebFetch
5
- VERSION = '0.7.2'
5
+ VERSION = '0.7.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask-web-fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto