mcp-scraper 0.96.0 → 0.96.2
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.
- package/CHANGELOG.md +28 -1
- package/README.md +3 -1
- package/dist/{actions-YCMJY3PF.js → actions-R4EJDLRO.js} +1 -1
- package/dist/bin/api-server.js +1 -1
- package/dist/bin/mcp-scraper-cli.js +1 -1
- package/dist/bin/mcp-scraper-core.js +1 -1
- package/dist/bin/mcp-scraper-install.js +1 -1
- package/dist/bin/mcp-stdio-server.js +1 -1
- package/dist/bin/paa-harvest.js +1 -1
- package/dist/{bundle-TTJP6DHU.js → bundle-FCXZMIRB.js} +1 -1
- package/dist/chunk-2OKQVTQ5.js +173 -0
- package/dist/{chunk-ZPQNY5TI.js → chunk-5LYINW7Z.js} +8 -1
- package/dist/{chunk-R5LJL6IR.js → chunk-6BD75VKS.js} +30 -37
- package/dist/{chunk-VAC3SQ5T.js → chunk-6K7ZJYQW.js} +3 -3
- package/dist/chunk-7H6YWD2L.js +1 -0
- package/dist/{chunk-6SNOUSSZ.js → chunk-7WPCGRD7.js} +1 -1
- package/dist/{chunk-DGAW7FD7.js → chunk-BPKTUCV7.js} +1 -1
- package/dist/{chunk-QWE7VKIX.js → chunk-CORWWDHL.js} +1 -1
- package/dist/{chunk-LFRAA3MY.js → chunk-DTVKZU5X.js} +1 -1
- package/dist/{chunk-HJIOH7HF.js → chunk-H6MODA7W.js} +2 -2
- package/dist/{chunk-JAV5V3JT.js → chunk-HVQZRQRJ.js} +19 -19
- package/dist/chunk-JHT6A4U6.js +11 -0
- package/dist/{chunk-ARSQZCTJ.js → chunk-KUEMW76G.js} +255 -245
- package/dist/{chunk-SNJR5N36.js → chunk-RYX4FJVH.js} +1 -1
- package/dist/{chunk-JW4GDY7S.js → chunk-SE4VKLEC.js} +1 -1
- package/dist/{chunk-TPN25AFZ.js → chunk-SFUOSJ3A.js} +1 -1
- package/dist/{chunk-GVYGAZV7.js → chunk-UXNR3K6I.js} +1 -1
- package/dist/{chunk-FRXKMOXI.js → chunk-ZTMQEDIB.js} +1 -1
- package/dist/{dispatch-3HY4735M.js → dispatch-P7IY75LH.js} +1 -1
- package/dist/index.cjs +36 -36
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -1
- package/dist/{queue-HUP2GNXF.js → queue-AC4XKD6Z.js} +1 -1
- package/dist/{reconciliation-ANXNJWME.js → reconciliation-UZUZ6B74.js} +1 -1
- package/dist/{server-VQU6S24A.js → server-AEEE7E3C.js} +600 -600
- package/dist/{settlement-6CCE2PDH.js → settlement-6CS4JZTY.js} +1 -1
- package/dist/{worker-5G5BK23B.js → worker-HHUU2ECG.js} +1 -1
- package/package.json +1 -1
- package/dist/chunk-2PRH4Z3N.js +0 -181
- package/dist/chunk-7UHDOIUE.js +0 -1
- package/dist/chunk-OB6E6UTD.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,31 @@ All notable changes to MCP Scraper are documented here. The format is based on [
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.96.2] - 2026-10-13
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Stop and drain PAA lease heartbeats before capturing and publishing completion, so a completed browser run can settle and report its saved result without falsely losing ownership.
|
|
12
|
+
- Refund the original customer hold after a confirmed failed direct SERP API attempt. Same-key recovery settles saved failure without another acquisition, while unresolved provider usage costs remain pending.
|
|
13
|
+
|
|
14
|
+
## [0.96.1] - 2026-10-13
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Wait at least 20 seconds between PAA progress checks, while preserving other operations' retry timing.
|
|
19
|
+
- Preserve complete loaded PAA answers, including lists, mathematical notation, entity names and source details. Continue discovering questions when their answers are already prefetched, and avoid repeated DOM reads and unnecessary or uncertain repeat clicks.
|
|
20
|
+
- Let managed CAPTCHA recovery finish within the original request deadline. Keep challenge and session traffic in the browser, and avoid unrelated searches during PAA-only extraction.
|
|
21
|
+
- Show initial People Also Ask questions, available answer snippets and source labels in full SERP text and structured responses. Distinguish these previews from fully expanded PAA answers.
|
|
22
|
+
- Run isolated test fixtures from clean checkouts without requiring one developer's path or branch, and validate Memory package layout against its current versions.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- Give acquisition diagnostics a dedicated scripts directory, move contributor guidance into documentation and keep generated output out of the source tree.
|
|
27
|
+
|
|
28
|
+
### Removed
|
|
29
|
+
|
|
30
|
+
- Remove the unused crawler UI prototype, broken root acquisition probes and tracked operating-system and Python cache files.
|
|
31
|
+
|
|
7
32
|
## [0.96.0] - 2026-10-12
|
|
8
33
|
|
|
9
34
|
### Security
|
|
@@ -2200,7 +2225,7 @@ All notable changes to MCP Scraper are documented here. The format is based on [
|
|
|
2200
2225
|
- Write actions remain unavailable until the account owner explicitly enables them.
|
|
2201
2226
|
- Provider-specific connection data is normalized into one agent-facing contract.
|
|
2202
2227
|
|
|
2203
|
-
[Unreleased]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.96.
|
|
2228
|
+
[Unreleased]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.96.2...HEAD
|
|
2204
2229
|
[0.95.2]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.95.1...v0.95.2
|
|
2205
2230
|
[0.95.1]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.95.0...v0.95.1
|
|
2206
2231
|
[0.95.0]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.94.4...v0.95.0
|
|
@@ -2370,3 +2395,5 @@ All notable changes to MCP Scraper are documented here. The format is based on [
|
|
|
2370
2395
|
[0.89.4]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.89.3...v0.89.4
|
|
2371
2396
|
[0.89.5]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.89.4...v0.89.5
|
|
2372
2397
|
[0.96.0]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.95.2...v0.96.0
|
|
2398
|
+
[0.96.1]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.96.0...v0.96.1
|
|
2399
|
+
[0.96.2]: https://github.com/VilovietaSEO/mcp-scraper/compare/v0.96.1...v0.96.2
|
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ deployment boundaries:
|
|
|
18
18
|
and [`product-topology.json`](product-topology.json) for exact ownership, source receipts, release units,
|
|
19
19
|
deployment roots, and verification commands.
|
|
20
20
|
|
|
21
|
+
Contributors can begin with [START-HERE.md](START-HERE.md) and the [codebase edit guide](docs/architecture/codebase-file-tree.md). Current product guidance lives in `docs/product/`, reference material in `docs/reference/`, and historical designs and diagnostics in `docs/archive/`. Manual acquisition probes live in `scripts/diagnostics/`; `bin/` contains package entrypoints.
|
|
22
|
+
|
|
21
23
|
## Install
|
|
22
24
|
|
|
23
25
|
Use the MCPB Desktop Extension for the branded Claude Desktop install, or use the npm package from any MCP client that can run stdio commands.
|
|
@@ -175,7 +177,7 @@ Build the branded one-click bundle:
|
|
|
175
177
|
npm run build:mcpb
|
|
176
178
|
```
|
|
177
179
|
|
|
178
|
-
The generated bundle is written to `build/mcpb/mcp-scraper-<version>.mcpb` and copied to `public/downloads/` for the hosted download. The current public bundle is `https://mcpscraper.dev/downloads/mcp-scraper.mcpb` (`0.96.
|
|
180
|
+
The generated bundle is written to `build/mcpb/mcp-scraper-<version>.mcpb` and copied to `public/downloads/` for the hosted download. The current public bundle is `https://mcpscraper.dev/downloads/mcp-scraper.mcpb` (`0.96.2`, SHA-256 `d2a753e28f042139dd009a7788b31418958c5d4e6cba1ba0ff6505154de6fd9e`). Install it by opening or dragging it into Claude Desktop. Claude displays the `MCP Scraper` install card, icon, API-key configuration field, and manually curated current-release message from the bundle manifest.
|
|
179
181
|
|
|
180
182
|
The MCPB install exposes every tool — web-intelligence plus all `browser_*` tools — through the one `mcp-scraper` server.
|
|
181
183
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{e as a,f as b,g as c,h as d,i as e,j as f,k as g,l as h}from"./chunk-
|
|
1
|
+
import{e as a,f as b,g as c,h as d,i as e,j as f,k as g,l as h}from"./chunk-SE4VKLEC.js";import"./chunk-SFUOSJ3A.js";import"./chunk-NALXKTUU.js";import"./chunk-6BD75VKS.js";import"./chunk-RDHGQ2WZ.js";import"./chunk-CORWWDHL.js";import"./chunk-5LYINW7Z.js";import"./chunk-SEZETSB3.js";import"./chunk-SDQW7IJN.js";import"./chunk-VVSHZV5S.js";import"./chunk-4ZVO6GX4.js";import"./chunk-WNUXMPVX.js";import"./chunk-WP3QS4CJ.js";import"./chunk-OILOHQNL.js";export{d as GMAIL_ATTACHMENT_REF_TTL_MS,c as GMAIL_INLINE_BODY_BYTES,b as GMAIL_MAIN_CONTRACT_HASH,a as GMAIL_MAIN_CONTRACT_VERSION,h as GmailService,e as GmailServiceError,g as normalizeGmailMessage,f as parseGmailAddresses};
|
package/dist/bin/api-server.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import{readFileSync as s}from"fs";function c(){try{for(let r of s(".env","utf8").split(`
|
|
3
|
-
`)){let o=r.indexOf("=");if(o<1||r.trimStart().startsWith("#"))continue;let e=r.slice(0,o).trim();process.env[e]||(process.env[e]=r.slice(o+1).trim())}}catch{}}c();async function a(){let[{serve:r},{app:o},{startWorker:e},{migrate:i}]=await Promise.all([import("@hono/node-server"),import("../server-
|
|
3
|
+
`)){let o=r.indexOf("=");if(o<1||r.trimStart().startsWith("#"))continue;let e=r.slice(0,o).trim();process.env[e]||(process.env[e]=r.slice(o+1).trim())}}catch{}}c();async function a(){let[{serve:r},{app:o},{startWorker:e},{migrate:i}]=await Promise.all([import("@hono/node-server"),import("../server-AEEE7E3C.js"),import("../worker-HHUU2ECG.js"),import("../core-schema-423BSTE6.js")]),n=parseInt(process.env.PORT??"3001");try{if(await i(),process.env.ANALYTICS_DATABASE_URL){let{migrateAnalytics:t}=await import("../migrations-3AKEC5QE.js");await t()}e(),r({fetch:o.fetch,port:n},t=>{console.log(`[server] http://localhost:${t.port}`),console.log(`[server] admin auth: ${process.env.ADMIN_KEY?"configured":"not configured"}`)})}catch(t){console.error("[startup] server preflight failed",t instanceof Error?t.name:"unknown_error"),process.exit(1)}}a();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{k as N,l as j,m as L,n as T,r as U}from"../chunk-4HZXATHB.js";import"../chunk-KJQXUZ4Y.js";import"../chunk-6N5EC7ZO.js";import{g as v,j as b,k as D,l as K,m as H}from"../chunk-24YDQ3EO.js";import"../chunk-OLYPLX5C.js";import"../chunk-B3JRL45T.js";import{c as _}from"../chunk-OILOHQNL.js";import{a as P}from"../chunk-
|
|
2
|
+
import{k as N,l as j,m as L,n as T,r as U}from"../chunk-4HZXATHB.js";import"../chunk-KJQXUZ4Y.js";import"../chunk-6N5EC7ZO.js";import{g as v,j as b,k as D,l as K,m as H}from"../chunk-24YDQ3EO.js";import"../chunk-OLYPLX5C.js";import"../chunk-B3JRL45T.js";import{c as _}from"../chunk-OILOHQNL.js";import{a as P}from"../chunk-7H6YWD2L.js";import{Command as he}from"commander";import{spawn as te}from"child_process";import{mkdir as ke,writeFile as Pe}from"fs/promises";import{basename as Ce,join as X}from"path";function ie(e){return e.apiKey?.trim()||"sk_live_your_key"}function ae(e){return e.packageSpec?.trim()||"mcp-scraper@latest"}function A(e={}){return["-y","--package",ae(e),"mcp-scraper"]}function se(e){let n={MCP_SCRAPER_API_KEY:ie(e)},c=e.browserProfileName?.trim();return c&&(n.BROWSER_AGENT_PROFILE_NAME=c),e.browserProfileSaveChanges===!0&&(n.BROWSER_AGENT_PROFILE_SAVE_CHANGES="true"),n}function q(){return["mcp","remove","mcp-scraper","-s","user"]}function B(){return["mcp","get","mcp-scraper"]}function J(e){let n=e.match(/^\s*Command:\s*(.+?)\s*$/m)?.[1];if(!n)return null;let c=e.match(/^\s*Args:\s*(.*?)\s*$/m)?.[1]??"",i=c.length?c.split(/\s+/):[],p={},u=e.split(/^\s*Environment:\s*$/m)[1];if(u)for(let a of u.split(`
|
|
3
3
|
`)){let l=a.match(/^\s{2,}([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);if(!l){if(a.trim().length&&!/^\s{2,}/.test(a))break;continue}p[l[1]]=l[2]}return{command:n,args:i,env:p}}function O(e){let n=["mcp","add","mcp-scraper","--scope","user"];for(let[c,i]of Object.entries(e.env))n.push("--env",`${c}=${i}`);return n.push("--",e.command,...e.args),n}function G(e={}){let n=["mcp","add","mcp-scraper","--scope","user"];for(let[c,i]of Object.entries(se(e)))n.push("--env",`${c}=${i}`);return n.push("--","npx",...A(e)),n}function I(e){if(e==="claude-code")return"claude";if(e==="claude"||D.hosts.some(n=>n.id===e))return e;throw new Error('Unknown host "'+e+'". Use: codex, claude, claude-code, claude-desktop, cursor, windsurf, cline, or user-action-only')}function ce(e){return K(e==="claude"?"claude-code":e)}function W(e,n={}){let c=I(e),i=ce(c),p="Restart the MCP client so it starts a fresh npx process.",u='MCP_SCRAPER_API_KEY="$MCP_SCRAPER_API_KEY" npx -y -p mcp-scraper@latest mcp-scraper-cli agent install claude --apply',a=`X-Ray install protocol: ${v} (${b})`;return c==="codex"?["# Codex MCP config",a,i.exactConfig,"",`Continuation: ${i.continuation}`,`Rollback: ${i.rollback}`,"",p].join(`
|
|
4
4
|
`):c==="claude"?["# Claude Code command",a,i.exactConfig,"","# One-command Claude Code setup",u,"",`Continuation: ${i.continuation}`,`Rollback: ${i.rollback}`,"",p].join(`
|
|
5
5
|
`):c==="claude-desktop"?["# Claude Desktop config",a,i.exactConfig,"","Desktop Extension: https://mcpscraper.dev/downloads/mcp-scraper.mcpb",`Continuation: ${i.continuation}`,`Rollback: ${i.rollback}`,p].join(`
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as e}from"../chunk-
|
|
2
|
+
import{a as e}from"../chunk-DTVKZU5X.js";import"../chunk-KUEMW76G.js";import"../chunk-QER2ELZP.js";import"../chunk-JHT6A4U6.js";import"../chunk-7POHNTVW.js";import"../chunk-MVMWPKRC.js";import"../chunk-63VWBMYR.js";import"../chunk-3F7AFM53.js";import"../chunk-RDHGQ2WZ.js";import"../chunk-2IATUJO6.js";import"../chunk-CORWWDHL.js";import"../chunk-5LYINW7Z.js";import"../chunk-CNSVZTTT.js";import"../chunk-5BKSKLHX.js";import"../chunk-7SR7MEU5.js";import"../chunk-HUNSEGUN.js";import"../chunk-SEZETSB3.js";import"../chunk-SDQW7IJN.js";import"../chunk-B4ZKPLSP.js";import"../chunk-VVSHZV5S.js";import"../chunk-C7MGJUTU.js";import"../chunk-4ZVO6GX4.js";import"../chunk-WNUXMPVX.js";import"../chunk-6K7ZJYQW.js";import"../chunk-WP3QS4CJ.js";import"../chunk-24YDQ3EO.js";import"../chunk-OLYPLX5C.js";import"../chunk-B3JRL45T.js";import"../chunk-OILOHQNL.js";import"../chunk-7H6YWD2L.js";import"../chunk-HNNYXMSI.js";var _=["harvest_paa","search_serp","extract_url","diff_page","map_site_urls","map_wayback_snapshots","extract_site","analyze_site_similarity","audit_site","check_site_export","site_export_read","site_export_image","archive_read","youtube_harvest","youtube_transcribe","facebook_page_intel","facebook_ad_search","reddit_thread","reddit_trending","video_frame_analysis","video_frame_analysis_status","facebook_ad_transcribe","google_ads_search","google_ads_page_intel","google_ads_transcribe","facebook_video_transcribe","instagram_profile_content","instagram_media_download","maps_place_intel","maps_search","trustpilot_reviews","g2_reviews","capture_serp_snapshot","capture_serp_page_snapshots"];e({toolsets:new Set(["paa","serp"]),allowedToolNames:_});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as s}from"../chunk-
|
|
2
|
+
import{a as s}from"../chunk-6K7ZJYQW.js";import"../chunk-WP3QS4CJ.js";import{a as e}from"../chunk-7H6YWD2L.js";var r=process.argv.includes("--no-color")||process.env.NO_COLOR!==void 0||process.env.FORCE_COLOR==="0"||!process.stdout.isTTY,n=process.argv.includes("--help")||process.argv.includes("-h");n&&(process.stdout.write(["Usage: mcp-scraper-install [--no-color]","","Prints the branded MCP Scraper terminal install card and copyable install commands.","mcp-scraper prints the same card in a human terminal and runs as the MCP stdio server in clients.",""].join(`
|
|
3
3
|
`)),process.exit(0));process.stdout.write(s({version:e,color:!r,apiKeyConfigured:!!process.env.MCP_SCRAPER_API_KEY?.trim()}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as r}from"../chunk-
|
|
2
|
+
import{a as r}from"../chunk-DTVKZU5X.js";import"../chunk-KUEMW76G.js";import"../chunk-QER2ELZP.js";import"../chunk-JHT6A4U6.js";import"../chunk-7POHNTVW.js";import"../chunk-MVMWPKRC.js";import"../chunk-63VWBMYR.js";import"../chunk-3F7AFM53.js";import"../chunk-RDHGQ2WZ.js";import"../chunk-2IATUJO6.js";import"../chunk-CORWWDHL.js";import"../chunk-5LYINW7Z.js";import"../chunk-CNSVZTTT.js";import"../chunk-5BKSKLHX.js";import"../chunk-7SR7MEU5.js";import"../chunk-HUNSEGUN.js";import"../chunk-SEZETSB3.js";import"../chunk-SDQW7IJN.js";import"../chunk-B4ZKPLSP.js";import"../chunk-VVSHZV5S.js";import"../chunk-C7MGJUTU.js";import"../chunk-4ZVO6GX4.js";import"../chunk-WNUXMPVX.js";import"../chunk-6K7ZJYQW.js";import"../chunk-WP3QS4CJ.js";import"../chunk-24YDQ3EO.js";import"../chunk-OLYPLX5C.js";import"../chunk-B3JRL45T.js";import"../chunk-OILOHQNL.js";import"../chunk-7H6YWD2L.js";import"../chunk-HNNYXMSI.js";r();
|
package/dist/bin/paa-harvest.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{J as t}from"../chunk-
|
|
2
|
+
import{J as t}from"../chunk-HVQZRQRJ.js";import"../chunk-M5QHXNFZ.js";import{d as r}from"../chunk-B4ZKPLSP.js";import"../chunk-VVSHZV5S.js";import"../chunk-C7MGJUTU.js";import"../chunk-4ZVO6GX4.js";import"../chunk-WNUXMPVX.js";import"../chunk-6N5EC7ZO.js";import"../chunk-HNNYXMSI.js";import{Command as s,Option as a}from"commander";var i=new s;i.name("paa-harvest").description("Recursively extract Google People Also Ask questions").requiredOption("-q, --query <query>","Seed query").option("-l, --location <location>",'Location name (e.g. "austin" or "Austin,Texas,United States")').option("--gl <gl>","Google country code","us").option("--hl <hl>","Google language code","en").option("-d, --depth <depth>","BFS depth (1-30)","3").option("-m, --max-questions <n>","Max questions to harvest","100").option("-o, --output <dir>","Output directory","./paa-output").option("-f, --format <format>","Output format: json, csv, or both","both").option("--headless","Run browser in headless mode",!1).option("--profile <dir>","Persistent browser profile directory").option("--proxy <url>","Proxy server URL").option("--browser-api-key <key>","Browser service API key (or set BROWSER_SERVICE_API_KEY env var)").addOption(new a("--\u006b\u0065\u0072\u006e\u0065\u006c-api-key <key>").hideHelp()).action(async e=>{try{let o=await t({query:e.query,location:e.location,gl:e.gl,hl:e.hl,depth:parseInt(e.depth,10),maxQuestions:parseInt(e.maxQuestions,10),outputDir:e.output,format:e.format,headless:e.headless,profileDir:e.profile,proxy:e.proxy,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:e.browserApiKey??e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey??r()});console.log(JSON.stringify({totalQuestions:o.totalQuestions,outputDir:o.stats.seed}))}catch(o){console.error(o instanceof Error?o.message:String(o)),process.exit(1)}});async function n(){await i.parseAsync()}n();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{j as b,k as c}from"./chunk-
|
|
1
|
+
import{j as b,k as c}from"./chunk-ZTMQEDIB.js";import"./chunk-U4WIJZCX.js";import{E as a}from"./chunk-7WPCGRD7.js";import"./chunk-KXJCGV3H.js";import"./chunk-JMZIDHYQ.js";import"./chunk-M5QHXNFZ.js";import"./chunk-CNSVZTTT.js";import"./chunk-5BKSKLHX.js";import"./chunk-7SR7MEU5.js";import"./chunk-HUNSEGUN.js";import"./chunk-SEZETSB3.js";import"./chunk-SDQW7IJN.js";import"./chunk-C7MGJUTU.js";import"./chunk-4ZVO6GX4.js";import"./chunk-WNUXMPVX.js";import"./chunk-OLYPLX5C.js";import"./chunk-B3JRL45T.js";import"./chunk-OILOHQNL.js";import"./chunk-HNNYXMSI.js";export{b as MAX_ANALYZED_LINK_EDGES,a as SITE_EXTRACT_PAGE_CHUNK,c as assembleExtractArtifacts};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import{e as pt,f as lr,g as ur,h as dr,i as cr}from"./chunk-6BD75VKS.js";import{a as _r}from"./chunk-U4WIJZCX.js";import{J as Fe,c as Ft,d as oe,e as K,f as Ut,k as Xt,l as Yt,s as er}from"./chunk-HVQZRQRJ.js";import{b as nr,c as sr,e as mt,g as gt,m as ue,n as ft,o as de,p as ce,q as ze}from"./chunk-JHT6A4U6.js";import{a as le,b as Ie,c as S,e as yr}from"./chunk-63VWBMYR.js";import{c as ir,d as ar,g as pr,k as mr}from"./chunk-5LYINW7Z.js";import{b as ie,d as ae,e as X,f as Y,g as fr,j as Q,p as We}from"./chunk-7SR7MEU5.js";import{d as Ue,e as $e,f as Je,g as Se,h as ne,k as Ae,m as se,o as Ge,r as Be,s as T}from"./chunk-HUNSEGUN.js";import{b as $t,c as Jt,d as Gt}from"./chunk-B4ZKPLSP.js";import{a as tr,b as rr,c as or}from"./chunk-VVSHZV5S.js";import{b as ct,d as Bt,e as Wt,g as U,h as B,j as zt,m as Vt,o as Kt}from"./chunk-C7MGJUTU.js";import{b as Qt,c as Zt}from"./chunk-4ZVO6GX4.js";import{a as m}from"./chunk-WNUXMPVX.js";import{b as gr}from"./chunk-B3JRL45T.js";import{c as O,d as h,f as Ee}from"./chunk-HNNYXMSI.js";import{randomUUID as Mo}from"crypto";async function Tn(e,t,r){let o=Mo();return await m().execute({sql:"INSERT INTO jobs (id, user_id, query, options, status, started_at) VALUES (?, ?, ?, ?, 'running', datetime('now'))",args:[o,e,t,JSON.stringify(r)]}),o}async function Cn(e,t,r,o){let n=await m().execute({sql:`INSERT INTO jobs (id, user_id, query, options, status, started_at)
|
|
2
|
+
VALUES (?, ?, ?, ?, 'running', datetime('now'))
|
|
3
|
+
ON CONFLICT(id) DO NOTHING`,args:[e,t,r,JSON.stringify(o)]}),i=await S(e,t);if(!i)throw new Error(`getOrCreateRunningJob: row missing immediately after insert/lookup for ${e}`);return{job:i,created:n.rowsAffected===1}}async function jn(e,t,r){await m().execute({sql:`UPDATE jobs
|
|
4
|
+
SET status = 'running', started_at = COALESCE(started_at, datetime('now'))
|
|
5
|
+
WHERE id = ? AND status = 'pending'
|
|
6
|
+
AND json_extract(options, '$.executionOwner') = ?
|
|
7
|
+
AND json_extract(options, '$.jobKind') = ?`,args:[e,t,r]});let o=await S(e);if(!(!o||o.options.executionOwner!==t||o.options.jobKind!==r))return o}async function qn(e=25){let t=Math.max(1,Math.min(100,Math.floor(e)));return(await m().execute({sql:`SELECT * FROM jobs
|
|
8
|
+
WHERE status = 'pending'
|
|
9
|
+
AND json_extract(options, '$.executionOwner') = 'inngest'
|
|
10
|
+
AND json_extract(options, '$.jobKind') = 'extract_url'
|
|
11
|
+
AND json_extract(options, '$.billingDebitKey') IS NOT NULL
|
|
12
|
+
AND created_at <= datetime('now', '-30 seconds')
|
|
13
|
+
AND datetime(COALESCE(json_extract(options, '$.extractUrlLastDispatchAt'), '1970-01-01')) <= datetime('now', '-30 seconds')
|
|
14
|
+
ORDER BY created_at
|
|
15
|
+
LIMIT ?`,args:[t]})).rows.map(o=>Ie(le(o)))}async function Mn(e,t){return(await m().execute({sql:`UPDATE jobs
|
|
16
|
+
SET options = json_set(options,
|
|
17
|
+
'$.extractUrlDispatchAttempts', COALESCE(json_extract(options, '$.extractUrlDispatchAttempts'), 0) + 1,
|
|
18
|
+
'$.extractUrlLastDispatchAt', ?,
|
|
19
|
+
'$.extractUrlLastDispatchError', ?)
|
|
20
|
+
WHERE id = ? AND status = 'pending'
|
|
21
|
+
AND json_extract(options, '$.executionOwner') = 'inngest'
|
|
22
|
+
AND json_extract(options, '$.jobKind') = 'extract_url'`,args:[new Date().toISOString(),t??null,e]})).rowsAffected===1}async function Ln(e){return(await m().execute({sql:"SELECT * FROM jobs WHERE user_id = ? ORDER BY created_at DESC LIMIT 50",args:[e]})).rows.map(r=>Ie(le(r)))}async function br(){let e=m(),t=await e.execute(`
|
|
23
|
+
SELECT * FROM jobs
|
|
24
|
+
WHERE status = 'pending'
|
|
25
|
+
AND COALESCE(json_extract(options, '$.executionOwner'), 'cron') NOT IN ('direct', 'inngest')
|
|
26
|
+
AND json_extract(options,'$.queueExecutionProtocol') IS NOT 0
|
|
27
|
+
ORDER BY created_at
|
|
28
|
+
LIMIT 1
|
|
29
|
+
`),r=t.rows[0]?le(t.rows[0]):void 0;return r?(await e.execute({sql:"UPDATE jobs SET status = 'running', started_at = datetime('now') WHERE id = ? AND status = 'pending'",args:[r.id]})).rowsAffected===0?void 0:{...r,status:"running"}:void 0}async function Hn(e,t){await m().execute({sql:`UPDATE jobs SET status = 'done', result = ?, error = NULL, public_error_json = NULL, completed_at = datetime('now') WHERE id = ? AND ${gt}`,args:[JSON.stringify(t),e]})}async function Fn(e,t,r){await m().execute({sql:`UPDATE jobs SET status = 'failed', error = ?, public_error_json = ?, completed_at = datetime('now') WHERE id = ? AND ${gt}`,args:[t,Bt(r),e]})}async function Gn(e=25){let t=Math.max(1,Math.min(100,Math.floor(e)));return(await m().execute({sql:`SELECT * FROM jobs
|
|
30
|
+
WHERE status IN ('done', 'failed', 'cancelled')
|
|
31
|
+
AND json_extract(options, '$.executionOwner') IN ('direct', 'inngest')
|
|
32
|
+
AND json_extract(options, '$.billingDebitKey') IS NOT NULL
|
|
33
|
+
AND CAST(json_extract(options, '$.billingHoldMc') AS INTEGER) > 0
|
|
34
|
+
AND json_extract(options, '$.billingSettledAt') IS NULL
|
|
35
|
+
ORDER BY completed_at DESC
|
|
36
|
+
LIMIT ?`,args:[t]})).rows.map(o=>Ie(le(o)))}async function hr(e,t=25,r="1"){if(!/^[a-z_]+$/.test(e)||!r.trim()||r.length>8192)throw Error("Invalid intent selection");return(await m().execute({sql:`SELECT r.id,r.user_id,r.job_id FROM operation_runs r
|
|
37
|
+
JOIN operation_executions e ON e.run_id=r.id
|
|
38
|
+
WHERE r.tool=? AND r.status='running' AND e.phase='claimed'
|
|
39
|
+
AND e.expires_at_ms <= CAST(strftime('%s','now') AS INTEGER)*1000
|
|
40
|
+
AND json_extract(r.normalized_flags_json,'$._storeGateRequired') IS 1 AND (${r})
|
|
41
|
+
ORDER BY r.started_at LIMIT ?`,args:[e,Math.max(1,Math.min(100,Math.floor(t)))]})).rows.map(n=>({runId:String(n.id),userId:Number(n.user_id),jobId:String(n.job_id)}))}import{request as $o}from"https";import{HttpsProxyAgent as Jo}from"https-proxy-agent";var Ve={source:"https://\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061.com/users/zone/premium_domains",retrievedAt:"2026-09-23T20:14:30.483Z",includedStatuses:["premium","remove_candidate"],domains:["advanceautoparts.com","affitto.it","agoda.cn","albertsons.com","allpeople.com","autozone.com","bestbuy.com","bestwestern.com","billiger.de","bottlerover.com","carousell.com","carousell.com.hk","carousell.com.my","carousell.ph","carousell.sg","carsales.com.au","cdiscount.com","chewy.com","costco.com","cvs.com","despegar.com.mx","dickssportinggoods.com","dynos.es","emaxme.com","familytreenow.com","feuvert.fr","flooranddecor.com","foodlion.com","footlocker.co.uk","footlocker.com","giantfoodstores.com","gopuff.com","gplay.bg","hermes.com","hyatt.com","idealo.de","immobilienscout24.de","ingatlan.com","instacart.com","intersport.fr","joann.com","kroger.com","lazada.co.id","lazada.co.th","lazada.com.my","lazada.com.ph","lazada.sg","lazada.vn","lowes.ca","lowes.com","mcmaster.com","mediamarkt.de","mediamarkt.es","medline.com","mscdirect.com","napaonline.com","nofrills.ca","peoplefinders.com","platt.com","publicdatausa.com","realcanadiansuperstore.ca","realestate.com.au","restaurantguru.com","searchpeoplefree.com","shopee.cl","shopee.co.id","shopee.co.th","shopee.com.br","shopee.com.co","shopee.com.mx","shopee.com.my","shopee.ph","shopee.sg","shopee.tw","shopee.vn","similarweb.com","skyscanner.co.kr","skyscanner.net","stopandshop.com","target.com","temu.com","ticketmaster.com","totalwine.com","tractorsupply.com","walmart.com.mx","wayfair.com","weismarkets.com","wizzair.com","worten.pt"]};var Vn=Ve.source,Kn=Ve.retrievedAt,Ho=new Set(Ve.domains);function Re(e){let t=new URL(e).hostname.toLowerCase().replace(/\.$/,"");for(let r of Ho)if(t===r||t.endsWith(`.${r}`))return"premium";return"standard"}function Ne(e){for(let t of["providerProduct","billingTier","billableEvent","unitType","source","policyVersion","effectiveAt"])if(!e[t]?.trim())throw new Error(`Provider rate ${t} is required`);if(!Number.isSafeInteger(e.numeratorUsdNanos)||e.numeratorUsdNanos<0)throw new Error("Provider rate numerator must be a non-negative safe integer");if(!Number.isSafeInteger(e.denominatorUnits)||e.denominatorUnits<1)throw new Error("Provider rate denominator must be a positive safe integer");if(Number.isNaN(Date.parse(e.effectiveAt)))throw new Error("Provider rate effectiveAt must be a date");return e}function Ke(e,t){if(Ne(e),!Number.isSafeInteger(t)||t<0)throw new Error("Billable units must be a non-negative safe integer");let r=BigInt(e.numeratorUsdNanos)*BigInt(t),o=BigInt(e.denominatorUnits),n=(r*2n+o)/(o*2n);if(n>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("Provider amount exceeds safe integer range");return Number(n)}function Fo(){return Ne({providerProduct:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_api",billingTier:"payg",billableEvent:"successful_delivery",unitType:"request",numeratorUsdNanos:15e8,denominatorUnits:1e3,source:"owner_confirmed_2026-09-24;docs/decisions/2026-08-11-preserve-questions-only-paa-while-evaluating-serp-api.md",policyVersion:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061-serp-payg-2026-09-24",effectiveAt:"2026-08-11T00:00:00Z"})}function vr(e=process.env){let t=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_CONTRACT_USD_PER_1000?.trim(),r=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_RATE_SOURCE?.trim(),o=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_RATE_POLICY_VERSION?.trim(),n=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_RATE_EFFECTIVE_AT?.trim();if(!t&&!r&&!o&&!n)return Fo();if(!t||!r||!o||!n)throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP contract rate requires amount, source, policy version, and effective date");let i=Number(t);if(!Number.isFinite(i)||i<0)throw new Error("Invalid \u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP contract amount");return Ne({providerProduct:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_api",billingTier:"payg",billableEvent:"successful_delivery",unitType:"request",numeratorUsdNanos:Math.round(i*1e9),denominatorUnits:1e3,source:r,policyVersion:o,effectiveAt:n})}function Uo(e){return Ne({providerProduct:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api",billingTier:e,billableEvent:"transferred_byte",unitType:"byte",numeratorUsdNanos:8e9,denominatorUnits:1e9,source:"owner_confirmed_2026-10-12",policyVersion:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061-browser-account-2026-10-12",effectiveAt:"2026-10-12T00:00:00Z"})}function Qn(e){return Uo(Re(e))}function wr(){return Ne({providerProduct:"apify_google_serp_scraperlink",billingTier:"store_pay_per_event",billableEvent:"serp_page",unitType:"page",numeratorUsdNanos:5e8,denominatorUnits:1e3,source:"https://apify.com/scraperlink/google-search-results-serp-scraper",policyVersion:"scraperlink-listing-2026-09-24",effectiveAt:"2026-09-24T00:00:00Z"})}var Go="https://api.\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061.com/request",Bo=8e3,Wo="brd.superproxy.io",zo="44445",Vo=10*1024*1024;async function Ko(e,t){let r=new URL(`http://${t.host}:${t.port}`);r.username=t.username,r.password=t.password;let o=new Jo(r);try{return await new Promise((n,i)=>{let s=$o(e,{agent:o,headers:{"x-unblock-data-format":"parsed"},rejectUnauthorized:!1,signal:t.signal},a=>{let l=[],u=0;a.on("data",d=>{let c=Buffer.isBuffer(d)?d:Buffer.from(d);if(u+=c.length,u>Vo){s.destroy(new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP response exceeded the maximum size"));return}l.push(c)}),a.on("end",()=>n({ok:(a.statusCode??500)>=200&&(a.statusCode??500)<300,status:a.statusCode??500,rawText:Buffer.concat(l).toString("utf8"),providerRequestId:typeof a.headers["x-response-id"]=="string"?a.headers["x-response-id"]:typeof a.headers["x-request-id"]=="string"?a.headers["x-request-id"]:null}))});s.on("error",()=>i(new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 native SERP request failed"))),s.end()})}finally{o.destroy()}}function Or(e){let t=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_USERNAME?.trim(),r=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_PASSWORD?.trim();if(t||r){if(!t||!r)throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP requires both \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_USERNAME and \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_PASSWORD");if(/[\u0000-\u001f\u007f]/.test(t+r))throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP native credentials contain invalid control characters");return{method:"native",username:t,password:r}}let o=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_API_KEY?.trim(),n=e.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_ZONE?.trim();if(!o||!n)throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP requires \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_API_KEY and \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_ZONE, or complete native SERP proxy credentials; \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_API_KEY is not used for SERP");if(/\s|[\u0000-\u001f\u007f]/.test(o)||!/^[a-zA-Z0-9_-]+$/.test(n))throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API key or zone has an invalid format");return{method:"rest",apiKey:o,zone:n}}function Ze(e=process.env){try{return Or(e),!0}catch{return!1}}function f(e){return e!=null&&typeof e=="object"&&!Array.isArray(e)?e:null}function q(e){return Array.isArray(e)?e:[]}function Z(e){return typeof e=="string"&&e.trim()?e.trim():null}function N(e){return typeof e=="number"&&Number.isFinite(e)?e:typeof e=="string"&&e.trim()&&Number.isFinite(Number(e))?Number(e):null}function y(e,t){for(let r of t){let o=Z(e[r]);if(o)return o}return null}function me(e){try{return new URL(e).hostname.replace(/^www\./,"")}catch{return""}}function Tr(e){return{rawUrl:e,resolvedUrl:e,linkType:"plain",resolutionStatus:"not_needed"}}function Xo(e){let t=e;if(typeof t=="string")try{t=JSON.parse(t)}catch{throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API returned invalid JSON")}let r=f(t);if(!r)throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API returned an invalid payload");if(typeof r.body=="string")try{let n=JSON.parse(r.body);if(f(n))return f(n)}catch{}let o=f(r.body);return o?f(o.result)??f(o.data)??o:f(r.result)??f(r.data)??r}function ge(e,t){for(let o of t){let n=q(e[o]);if(n.length)return n}let r=f(e.results);if(r)for(let o of t){let n=q(r[o]);if(n.length)return n}return[]}function pe(e,t){let r=[e,f(e.results)].filter(o=>!!o);for(let o of r)for(let n of t)if(Object.prototype.hasOwnProperty.call(o,n))return{observed:!0,rawShapeValid:Array.isArray(o[n]),rawCount:q(o[n]).length};return{observed:!1,rawShapeValid:!1,rawCount:0}}function _t(e,t){let r=[e,f(e.results)].filter(o=>!!o);for(let o of r)for(let n of t){if(!Object.prototype.hasOwnProperty.call(o,n))continue;let i=f(o[n]);return{observed:!0,rawShapeValid:i!==null,rawCount:i&&Object.keys(i).length>0?1:0}}return{observed:!1,rawShapeValid:!1,rawCount:0}}function Er(e){return ge(e,["organic","organic_results","organicResults"]).flatMap((t,r)=>{let o=f(t);if(!o)return[];let n=y(o,["link","url","href"]),i=y(o,["title","name"]);if(!n||!i)return[];let s=f(o.rating)??f(o.inline_rating);return[{position:Math.max(1,Math.trunc(N(o.global_rank)??N(o.rank)??N(o.position)??r+1)),title:i,url:n,...Tr(n),domain:me(n),cite:y(o,["display_link","displayLink","cite"]),snippet:y(o,["description","snippet","text"]),isRedditStyle:me(n)==="reddit.com",inlineRating:s?{value:String(s.value??s.rating??""),count:String(s.count??s.reviews??"")}:null}]}).sort((t,r)=>t.position-r.position)}function Sr(e){return ge(e,["snack_pack","local","local_results","local_pack","localPack"]).flatMap((t,r)=>{let o=f(t);if(!o)return[];let n=y(o,["title","name"]);if(!n)return[];let i=q(o.extensions??o.metadata).map(Z).filter(s=>!!s);return[{position:Math.max(1,Math.trunc(N(o.global_rank)??N(o.rank)??N(o.position)??r+1)),name:n,cid:y(o,["cid"]),rating:y(o,["rating"])??(N(o.rating)!=null?String(N(o.rating)):null),reviewCount:y(o,["reviews_cnt","reviews","review_count","reviewCount"])??(N(o.reviews_cnt)!=null?String(N(o.reviews_cnt)):N(o.reviews)!=null?String(N(o.reviews)):null),metadata:i,websiteUrl:y(o,["website","website_url","link"]),directionsUrl:y(o,["directions","directions_url"])}]})}function Cr(e){let t=f(e);return q(t?.citations??t?.sources??t?.references).flatMap(r=>{let o=f(r);if(!o)return[];let n=y(o,["link","url","href"]);return n?[{text:y(o,["title","text","name"])??me(n),href:n,...Tr(n)}]:[]})}function Qe(e,t=0){if(t>4)return[];if(Array.isArray(e))return e.flatMap(i=>Qe(i,t+1));let r=f(e);if(!r)return[];let o=y(r,["snippet","text","answer"]),n=Qe(r.texts??r.list??r.items,t+1);return[...o?[o]:[],...n]}function Ar(e,t){if(!t)return{detected:!1,text:null,citations:[]};let r=f(e.ai_overview)??f(e.aiOverview)??f(e.overview);if(!r)return{detected:!1,text:null,citations:[]};let o=y(r,["text","answer","description"])??(Qe(r.texts).join(`
|
|
42
|
+
|
|
43
|
+
`).slice(0,5e4)||null),n=Cr(r);return{detected:!!(o||n.length),text:o,citations:n}}function Ir(e,t){if(!t)return{detected:!1,text:null,citations:[]};let r=f(e.ai_mode)??f(e.aiMode);if(!r)return{detected:!1,text:null,citations:[]};let o=y(r,["text","answer","description"])??(Qe(r.texts).join(`
|
|
44
|
+
|
|
45
|
+
`).slice(0,5e4)||null),n=Cr(r);return{detected:!!(o||n.length),text:o,citations:n}}function Rr(e,t){return t?[...ge(e,["videos","video_results"]),...ge(e,["short_videos","shorts"])].flatMap(r=>{let o=f(r);if(!o)return[];let n=y(o,["link","url"]),i=y(o,["title","name"]);return!n||!i?[]:[{type:"video",title:i,channel:y(o,["channel","source"])??"",platform:me(n),duration:y(o,["duration"])??"",url:n}]}):[]}function Nr(e,t){if(!t)return[];let r=f(e.forums);return[...ge(e,["discussions","forums","forum_results"]),...q(r?.items)].flatMap(o=>{let n=f(o);if(!n)return[];let i=y(n,["link","url"]),s=y(n,["title","name"]);return!i||!s?[]:[{title:s,source:y(n,["source"])??me(i),url:i}]})}function kr(e,t){return t?ge(e,["what_people_are_saying","whatPeopleSaying"]).flatMap(r=>{let o=f(r);if(!o)return[];let n=y(o,["link","url"]),i=y(o,["title","name"]);if(!n||!i)return[];let s=y(o,["platform","source"])??me(n),a=s.toLowerCase();return[{type:a.includes("reddit")?"reddit":a.includes("facebook")?"facebook":a.includes("instagram")?"instagram":a.includes("tiktok")?"tiktok":a.includes("youtube")?"youtube":a.includes("news")?"news":"unknown",title:i,url:n,source:s,platform:s,popularComment:y(o,["comment","popular_comment"]),engagement:y(o,["engagement"])??"",date:y(o,["date"])??"",duration:y(o,["duration"]),authorNote:y(o,["author_note"])}]}):[]}var Yo={featuredSnippets:["featured_snippets","featured_snippet","featuredSnippet"],knowledgePanel:["knowledge","knowledge_graph","knowledgeGraph"],topStories:["top_stories","topStories"],news:["news","news_results"],images:["images","image_results"],shopping:["shopping","shopping_results"],relatedSearches:["related","related_searches","relatedSearches"],recipes:["recipes","recipe_results"],jobs:["jobs","job_results"],topAds:["top_ads"],bottomAds:["bottom_ads"],topProducts:["top_pla"],bottomProducts:["bottom_pla"],jackpotProducts:["jackpot_pla"],shortVideos:["short_videos","shorts"],localMap:["snack_pack_map"],flights:["flights"],hotels:["hotels_selection"],popularProducts:["popular_products"],weather:["weather"],currencyExchange:["currency_exchange"],latestPosts:["latest_posts"],perspectives:["perspectives"],navigation:["navigation"],chips:["chips"],spelling:["spelling"]};function Xe(e,t){for(let r of[e,f(e.results)])if(r){for(let o of t)if(Object.prototype.hasOwnProperty.call(r,o))return{observed:!0,value:r[o]}}return{observed:!1,value:null}}function Ye(e,t,r){return e?t==null||Array.isArray(t)&&t.length===0?"observed_absent":Array.isArray(t)?r>0?"observed_present":"incomplete":f(t)?r>0?"observed_present":Object.keys(f(t)).length===0?"observed_absent":"incomplete":"incomplete":"unknown"}function Qo(e){let t=y(e,["answer","snippet","text"]);return t||q(e.answers).flatMap((o,n)=>{let i=f(o),s=f(i?.value),a=i?.type==="answer"?Z(s?.text):null;return a?[{prose:a,rank:N(i?.rank)??n+1,index:n}]:[]}).sort((o,n)=>o.rank-n.rank||o.index-n.index).map(o=>o.prose).join(`
|
|
46
|
+
|
|
47
|
+
`)||null}function Zo(e,t){let r={},o=[["localPack",["snack_pack","local","local_results","local_pack","localPack"],t.localPack],["forums",["discussions","forums","forum_results"],t.forums],["videos",["videos","video_results","short_videos","shorts"],t.videos],["aiOverview",["ai_overview","aiOverview","overview"],t.aiOverview],["whatPeopleSaying",["what_people_are_saying","whatPeopleSaying"],t.whatPeopleSaying],["entityIds",["knowledge","knowledge_graph","knowledgeGraph"],t.entityIds]];for(let[l,u,d]of o){let c=Xe(e,u);r[l]=Ye(c.observed,c.value,d)}let n=Xe(e,["ai_mode","aiMode"]);r.aiMode=Ye(n.observed,n.value,t.aiMode);let i=Xe(e,["people_also_ask","peopleAlsoAsk","related_questions","paa"]),s=q(i.value).slice(0,30).flatMap(l=>{let u=f(l);if(!u)return[];let d=y(u,["question","title"]);if(!d)return[];let c=Z(u.question)?Z(u.title)?.slice(0,1e3):null,p=Z(u.answer_source)?.slice(0,1e3),b=y(u,["answer_display_link","display_link"])?.slice(0,2e3);return[{question:d.slice(0,1e3),answer:Qo(u)?.slice(0,1e4)??null,url:y(u,["answer_link","link","url"])??null,...c?{sourceTitle:c}:{},...p?{sourceName:p}:{},...b?{displayLink:b}:{}}]});r.paaPreview=Ye(i.observed,i.value,s.length);let a=[];for(let[l,u]of Object.entries(Yo)){let d=Xe(e,u);if(!d.observed)continue;let c=f(d.value),b=(Array.isArray(d.value)?d.value:c&&(Array.isArray(c.items)||Array.isArray(c.posts))?[...q(c.items),...q(c.posts)]:[d.value]).slice(0,20).flatMap(_=>{let w=f(_);if(!w)return[];let k=y(w,["title","name","heading"]),H=y(w,["link","url","website"]),J=y(w,["description","snippet","text","summary","story"]);return!k&&!H&&!J?[]:[{title:k?.slice(0,1e3)??null,url:H,description:J?.slice(0,1e4)??null,position:N(w.global_rank)??N(w.rank)??N(w.position)??null}]});r[l]=Ye(d.observed,d.value,b.length);let v=Object.fromEntries(Object.entries(c??{}).slice(0,30).flatMap(([_,w])=>/^[a-zA-Z0-9_]{1,80}$/.test(_)&&(w===null||["string","number","boolean"].includes(typeof w))?[[_,typeof w=="string"?w.slice(0,2e3):w]]:[]));a.push({name:l,attributes:v,items:b})}return{status:r,paaPreview:s,additional:a}}function xr(e,t){let r=new Set,o=new Set(t.map(s=>s.cid).filter(s=>!!s)),n=new Set,i=(s,a=0)=>{if(a>6||s==null)return;if(Array.isArray(s)){for(let u of s)i(u,a+1);return}let l=f(s);if(l)for(let[u,d]of Object.entries(l)){let c=u.toLowerCase().replace(/[^a-z]/g,""),p=Z(d);p&&(c==="kgid"||c==="kgmid"||c==="mid")?r.add(p):p&&c==="cid"?o.add(p):p&&c==="gcid"?n.add(p):typeof d=="object"&&i(d,a+1)}};return i(e),{entities:[],kgIds:[...r],cids:[...o],gcids:[...n]}}function Dr(e,t){let r=e instanceof Error?e.message:String(e);return t.aborted||/timeout|timed out/i.test(r)?"timeout":/captcha|recaptcha|unusual traffic|google\.com\/sorry|blocked/i.test(r)?"captcha":/invalid (?:json|payload)/i.test(r)?"invalid_payload":"provider_error"}function Pr(e){return e==="invalid_payload"?"invalid_payload":e==="captcha"?"serp_readiness":e==="timeout"||e==="provider_error"?"provider_transport":"pagination"}async function yt(e,t=process.env,r=fetch,o=Ko,n){if(e.pages!==1)throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP supports one page only; use Apify for two pages");let i=Or(t),s=i.method==="native",a=Date.now(),l=Zt(),u=null,d=e.location?Yt(e.location):null;if(e.location&&!d?.canonical)throw new ue("query_integrity","location_unresolved",`SERP location could not be resolved without changing the query (candidates: ${d?.ambiguousCandidates??0})`);let c=d?.canonical?Xt(d.canonical):null,p=AbortSignal.timeout(e.includeAllSerpFeatures?33e3:Bo),b=e.signal?AbortSignal.any([e.signal,p]):p,v=1,_=[],w=[],k=0,H=async g=>{let P=Date.now(),R=s?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_native_proxy":"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_rest",C=null,G=null,be=!1,he=!1,re=null,ve=null;if(l?.operationRunId&&(g===1||u))try{C=await ie({runId:l.operationRunId,attemptNumber:n?n.attemptNumber+g-1:g,method:R,provider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",...g===2?{parentAttemptId:u,edgeKind:"next_phase"}:n?{parentAttemptId:n.parentAttemptId,edgeKind:"provider_fallback",retryKind:"provider_fallback"}:{},normalizedFlags:{page:g,device:e.device,hasLocation:!!e.location}}),g===1&&(u=C),G=(await X({attemptId:C,provider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",providerProduct:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_api",accountScope:l.operationAccountScope??"production-default",method:R,providerStatus:"started"})).id}catch(A){throw console.warn(JSON.stringify({event:"direct_serp_telemetry_start_failed",page:g,message:A instanceof Error?A.message:String(A)})),A}let x=new URLSearchParams({q:e.query,gl:e.gl,hl:e.hl,pws:"0",num:"10",...g===2?{start:"10"}:{}}),He=$t(e.recency);He&&x.set("tbs",He),e.device==="mobile"&&x.set("brd_mobile","1"),e.includeAiOverview&&x.set("brd_ai_overview","2"),c&&x.set("uule",c),s&&x.set("brd_json","1"),k+=1;let F;try{if(i.method==="native"){let D=t.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_HOST?.trim()||Wo,we=t.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_PROXY_PORT?.trim()||zo;F=await o(`https://www.google.com/search?${x}`,{username:i.username,password:i.password,host:D,port:we,signal:b})}else{let D=await r(Go,{method:"POST",headers:{Authorization:`Bearer ${i.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify({zone:i.zone,url:`https://www.google.com/search?${x}`,format:"json",data_format:"parsed"}),signal:b});F={ok:D.ok,status:D.status,rawText:await D.text(),providerRequestId:D.headers.get("x-response-id")??D.headers.get("x-request-id")}}if(ve=`http_${F.status}`,G&&F.providerRequestId&&await Y(G,F.providerRequestId),!F.ok)throw new Error(`\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API failed with HTTP ${F.status}`);be=!0;let A;try{A=Xo(JSON.parse(F.rawText))}catch(D){let we=D instanceof Error&&D.message!=="Unexpected end of JSON input"?D.message:"\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API returned invalid JSON";throw new ue("invalid_payload","invalid_provider_payload",we)}let E=Er(A);return _.push({page:g,attempted:!0,captured:E.length>0,resultCount:E.length,requestOffset:g===2?10:0,providerRequestIdObserved:!!F.providerRequestId,...E.length===0?{failureCode:"empty_page"}:{}}),he=!0,A}catch(A){re=A;let E=Dr(A,b);if(_.some(we=>we.page===g)||_.push({page:g,attempted:!0,captured:!1,resultCount:0,requestOffset:g===2?10:0,providerRequestIdObserved:!1,failureCode:E}),g===2)throw A;let D=A instanceof ue?A.stage:Pr(E);throw A instanceof ue?A:new ue(D,E,A instanceof Error?A.message:String(A))}finally{let A=null;if(G&&be)try{let E=vr(t);await Q({receiptId:G,costState:"measured",units:1,unitType:"request",amountUsdNanos:Ke(E,1),rateSource:E.source,providerProduct:E.providerProduct,billingTier:E.billingTier,billableEvent:E.billableEvent,measurementSource:"provider_http_delivery",rateNumerator:E.numeratorUsdNanos,rateDenominator:E.denominatorUnits,ratePolicyVersion:E.policyVersion,rateEffectiveAt:E.effectiveAt,providerStatus:ve??"delivered",providerDurationMs:Date.now()-P})}catch(E){console.warn(JSON.stringify({event:"direct_serp_receipt_finalize_failed",page:g,message:E instanceof Error?E.message:String(E)})),A=E}if(C&&await ae({attemptId:C,status:he?"succeeded":b.aborted?"timed_out":"failed",errorCode:re?Dr(re,b):null,durationMs:Date.now()-P}).catch(E=>console.warn(JSON.stringify({event:"direct_serp_attempt_finish_failed",page:g,message:E instanceof Error?E.message:String(E)}))),A)throw A}};try{w.push({page:1,payload:await H(1)})}catch(g){throw g}let J=null;if(v===2)try{w.push({page:2,payload:await H(2)})}catch(g){J=g}let at=Date.now()-a,Ce=Date.now(),I=w[0].payload,je=w.flatMap(({page:g,payload:P})=>Er(P).map(R=>({...R,position:g===2&&R.position<=10?R.position+10:R.position,sourcePage:g}))).filter((g,P,R)=>R.findIndex(C=>ft(C.url)===ft(g.url))===P),qe=e.includeLocalPack||e.includeAllSerpFeatures,V=qe?Sr(I):[],ye=pe(I,["snack_pack","local","local_results","local_pack","localPack"]),Po=ce({requested:qe,moduleObserved:ye.observed,rawCount:ye.rawCount,mappedCount:V.length,rawShapeValid:ye.rawShapeValid}),xt=e.includeAiOverview||e.includeAllSerpFeatures,Dt=Ar(I,xt),Pt=e.includeForums||e.includeAllSerpFeatures,Ot=Nr(I,Pt),Tt=e.includeVideos||e.includeAllSerpFeatures,Ct=Rr(I,Tt),jt=e.includeWhatPeopleSaying||e.includeAllSerpFeatures,qt=kr(I,jt),lt=f(I.forums),ut=lt&&Array.isArray(lt.items)?{observed:!0,rawCount:lt.items.length,rawShapeValid:!0}:pe(I,["discussions","forums","forum_results"]),dt=pe(I,["videos","video_results","short_videos","shorts"]),Mt={localPack:Po,forums:ce({requested:Pt,moduleObserved:ut.observed,rawCount:ut.rawCount,mappedCount:Ot.length,rawShapeValid:ut.rawShapeValid}),videos:ce({requested:Tt,moduleObserved:dt.observed,rawCount:dt.rawCount,mappedCount:Ct.length,rawShapeValid:dt.rawShapeValid}),aiOverview:ce({requested:xt,moduleObserved:_t(I,["ai_overview","aiOverview","overview"]).observed,rawCount:_t(I,["ai_overview","aiOverview","overview"]).rawCount,mappedCount:Dt.detected?1:0,rawShapeValid:_t(I,["ai_overview","aiOverview","overview"]).rawShapeValid}),whatPeopleSaying:ce({requested:jt,moduleObserved:pe(I,["what_people_are_saying","whatPeopleSaying"]).observed,rawCount:pe(I,["what_people_are_saying","whatPeopleSaying"]).rawCount,mappedCount:qt.length,rawShapeValid:pe(I,["what_people_are_saying","whatPeopleSaying"]).rawShapeValid})},Oo=de(1,_),To=_.find(g=>g.page===1)?.resultCount??0,Me=_.find(g=>g.page===2)?.resultCount??0,Co=ze({query:e.query,finalQuery:e.query,canonicalLocation:d?.canonical??null,locationResolutionSource:d?.source??"not_requested",locationAmbiguousCandidates:d?.ambiguousCandidates??0,uule:c,outboundProviderMethod:s?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_native_proxy":"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_rest"}),Le=J?Pr(_.find(g=>g.page===2)?.failureCode??"provider_error"):Object.values(Mt).some(g=>g==="incomplete"||g==="unknown")?"optional_feature_parsing":null,jo={pagination:{...Oo,providerRequestCount:k},features:Mt,queryIntegrity:Co,failureStage:Le},Lt=e.includeAllSerpFeatures?(()=>{let g=w.map(({page:P,payload:R})=>{let C=Sr(R),G=Nr(R,!0),be=Rr(R,!0),he=kr(R,!0),re=Ar(R,!0),ve=Ir(R,!0),x=xr(R,C),He=Zo(R,{localPack:C.length,forums:G.length,videos:be.length,aiOverview:re.detected?1:0,aiMode:ve.detected?1:0,whatPeopleSaying:he.length,entityIds:x.entities.length+x.kgIds.length+x.cids.length+x.gcids.length});return{page:P,...He,localPack:C,forums:G,videos:be,whatPeopleSaying:he,aiOverview:re,aiMode:ve,entityIds:x}});return{status:g[0].status,paaPreview:g.flatMap(P=>P.paaPreview),additional:g.flatMap(P=>P.additional),pages:g}})():void 0,qo=new Date().toISOString(),Ht=Date.now()-a;return{seed:e.query,location:e.location??null,extractedAt:qo,diagnostics:{phaseTimings:{providerConnectMs:at,serpParseMs:Date.now()-Ce,totalServerMs:Ht},pagination:{requestedPages:v,capturedPages:v===2&&Me>0?2:1,page2Status:v===2?Me>0?"captured":"failed":"not_requested",page1OrganicCount:To,page2OrganicCount:Me,...v===2&&Me===0?{failureCode:_.find(g=>g.page===2)?.failureCode==="captcha"?"captcha":_.find(g=>g.page===2)?.failureCode==="timeout"?"timeout":"empty_page"}:{}},completionStatus:"serp_only",problem:null,resultQuality:Le?"partial":"complete",degradedResult:!!Le,degradationReasons:[],retryRecommended:!!Le,serpCompleteness:jo},totalQuestions:0,surface:"web",aiOverview:Dt,aiMode:Ir(I,e.includeAllSerpFeatures),...Lt?{fullSerpFeatures:Lt}:{},whatPeopleSaying:qt,tree:[],flat:[],videos:Ct,forums:Ot,organicResults:je,localPack:V,entityIds:xr(I,V),stats:{seed:e.query,totalQuestions:0,maxDepthReached:0,durationMs:Ht,errorCount:0}}}var en="scraperlink~google-search-results-serp-scraper",ke="https://api.apify.com/v2";var M=class extends Error{constructor(r,o=null,n=!1,i=null){super(r);this.runId=o;this.retrySafe=n;this.terminalStatus=i;this.name="ApifyGoogleRunError"}runId;retrySafe;terminalStatus};function xe(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)?e:null}function De(e){return typeof e=="string"&&e.trim()?e.trim():null}function tn(e){try{return["http:","https:"].includes(new URL(e).protocol)}catch{return!1}}function rn(e){if(typeof e!="string")return e;try{return JSON.parse(e)}catch{throw new Error("Apify searchResults is not valid JSON")}}function jr(e,t){if(!Array.isArray(e)||e.length===0)throw new Error("Apify dataset contains no search results");let r=[];for(let[i,s]of e.entries()){let a=xe(s);if(!a)throw new Error("Apify dataset contains an invalid row");let l=rn(a.searchResults??a),u=xe(l),d=Array.isArray(l)?l:Array.isArray(u?.results)?u.results:Array.isArray(u?.organicResults)?u.organicResults:Array.isArray(u?.organic)?u.organic:u&&(u.position!==void 0||u.title!==void 0)?[u]:null;if(!d)throw new Error("Apify dataset search result shape is unsupported");let c=Number(a.page_number??u?.page_number??i+1);if(!Number.isInteger(c)||c<1||c>t)throw new Error("Apify dataset has an unexpected page number");r.push(...d.map(p=>({row:p,page:c})))}let o=r.map(({row:i,page:s},a)=>{let l=xe(i),u=l?.position,d=De(l?.title),c=De(l?.url??l?.link);if(!l||!d||!c||!tn(c)||u!==void 0&&(!Number.isInteger(u)||u<1))throw new Error(`Apify search result ${a+1} is missing a valid position, URL, or title`);let p=De(l.description??l.snippet),b=typeof u=="number"?u:a%10+1;return{position:s>1&&b<=10?b+(s-1)*10:b,url:c,title:d,description:p}});if(o.length>t*10)throw new Error("Apify returned more results than requested");let n={page1:o.filter(i=>i.position<=10).length,page2:o.filter(i=>i.position>10).length};if(n.page1===0)throw new Error("Apify did not return the first result page");return{results:o,pageResultCounts:n}}async function et(e,t,r,o){let n=await r(e,{headers:{Authorization:`Bearer ${t}`},signal:o});if(!n.ok)throw new Error(`Apify API returned HTTP ${n.status}`);return n.json()}function bt(e){let t=xe(xe(e)?.data),r=De(t?.id),o=De(t?.status);if(!r||!o)throw new Error("Apify returned an invalid run receipt");return{id:r,status:o}}async function on(e,t=fetch){let r=performance.now(),o=AbortSignal.timeout(9e4),n=e.signal?AbortSignal.any([e.signal,o]):o,i=e.runId,s=!1,a=null;try{if(!/^[A-Za-z0-9]+$/.test(i))throw new Error("Invalid Apify run ID");let l=bt(await et(`${ke}/actor-runs/${i}?waitForFinish=30`,e.token,t,n));for(;["READY","RUNNING","TIMING-OUT","ABORTING"].includes(l.status);)l=bt(await et(`${ke}/actor-runs/${i}?waitForFinish=30`,e.token,t,n));if(s=!0,a=l.status,l.status!=="SUCCEEDED")throw new Error(`Apify run ended with status ${l.status}`);let u=e.pages??1,d=await et(`${ke}/actor-runs/${i}/dataset/items?format=json`,e.token,t,n);return{query:e.query,...jr(d,u),requestedPages:u,runId:i,elapsedMs:Math.round(performance.now()-r)}}catch(l){throw new M(l instanceof Error?l.message:String(l),i,s,a)}}async function qr(e,t,r=fetch){let o=performance.now(),n=e.query.trim(),i=e.pages??1;if(!n||n.includes(`
|
|
48
|
+
`))throw new Error("Apify search requires one nonempty query");if(i!==1&&i!==2)throw new Error("Apify search supports one or two pages");let s=t.token.trim();if(!s)throw new Error("Apify API token is required");let a=t.maxChargeUsd??.01;if(!Number.isFinite(a)||a<=0)throw new Error("Invalid Apify charge cap");let l=AbortSignal.timeout(9e4),u=e.signal?AbortSignal.any([e.signal,l]):l,d=`${ke}/actors/${en}/runs?maxTotalChargeUsd=${a}&restartOnError=false&waitForFinish=30`,c=null,p=!1,b=null;try{let v=await r(d,{method:"POST",headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"},body:JSON.stringify({keyword:n,limit:i===2?"20":"10",page:1,proxy_location:"us",include_merged:!1}),signal:u});if(!v.ok)throw new Error(`Apify run start returned HTTP ${v.status}`);let _=bt(await v.json());if(c=_.id,p=!["READY","RUNNING","TIMING-OUT","ABORTING"].includes(_.status),p&&(b=_.status),_.status!=="SUCCEEDED"&&_.status!=="READY"&&_.status!=="RUNNING")throw new Error(`Apify run ended with status ${_.status}`);if(_.status==="SUCCEEDED"){let k=await et(`${ke}/actor-runs/${c}/dataset/items?format=json`,s,r,u);return{query:n,...jr(k,i),requestedPages:i,runId:c,elapsedMs:Math.round(performance.now()-o)}}return{...await on({runId:c,query:n,token:s,pages:i,signal:u},r),elapsedMs:Math.round(performance.now()-o)}}catch(v){throw v instanceof M?v:new M(v instanceof Error?v.message:String(v),c,p,b)}}var Mr="apify_google_serp",Lr="apify",nn="apify_google_serp_scraperlink";async function Hr(e,t,r,o){let n=wr();await Q({receiptId:e,costState:"estimated",units:t,unitType:n.unitType,amountUsdNanos:Ke(n,t),rateSource:n.source,providerProduct:n.providerProduct,billingTier:n.billingTier,billableEvent:n.billableEvent,measurementSource:"actor_run_succeeded",rateNumerator:n.numeratorUsdNanos,rateDenominator:n.denominatorUnits,ratePolicyVersion:n.policyVersion,rateEffectiveAt:n.effectiveAt,providerStatus:o,providerDurationMs:r})}async function Fr(e){let t=performance.now(),r=await ie({runId:e.operationRunId,attemptNumber:e.attemptNumber,method:Mr,provider:Lr,parentAttemptId:e.parentAttemptId,edgeKind:e.edgeKind,retryKind:e.retryKind,normalizedFlags:{page:1,pages:e.options.pages??1,proxyLocation:"us"}}),o=await X({attemptId:r,provider:Lr,providerProduct:nn,billingTier:"store_pay_per_event",accountScope:e.accountScope,method:Mr,providerStatus:"before_actor_start"}),n=null,i=null;try{return n=await qr(e.options,{token:e.token},e.fetchImpl),await Y(o.id,n.runId),await Hr(o.id,n.requestedPages,n.elapsedMs,"succeeded_estimated"),{...n,attemptId:r}}catch(s){throw i=s,n?Object.assign(new M("Apify cost tracking failed after run completion",n.runId),{attemptId:r}):(s instanceof M&&s.runId&&(await Y(o.id,s.runId),s.terminalStatus==="SUCCEEDED"?await Hr(o.id,e.options.pages??1,Math.round(performance.now()-t),"succeeded_dataset_unavailable"):await fr({receiptId:o.id,units:e.options.pages??1,unitType:"page",providerStatus:"run_outcome_or_cost_pending"})),Object.assign(s instanceof Error?s:new Error(String(s)),{attemptId:r}))}finally{await ae({attemptId:r,status:i?e.options.signal?.aborted?"timed_out":"failed":"succeeded",errorCode:i?"apify_search_failed":null,durationMs:Math.round(performance.now()-t)})}}function Ur(e,t=e.requestedPages){let r=e.results.map(s=>{let a=new URL(s.url).hostname.replace(/^www\./,"");return{position:s.position,title:s.title,url:s.url,rawUrl:s.url,resolvedUrl:s.url,linkType:"plain",resolutionStatus:"not_needed",domain:a,cite:null,snippet:s.description,isRedditStyle:a==="reddit.com",inlineRating:null}}),o=t===2&&e.pageResultCounts.page2>0?2:1,n=t===2&&o===1,i=de(t,[{page:1,attempted:!0,captured:!0,resultCount:e.pageResultCounts.page1,requestOffset:0,providerRequestIdObserved:!0},...t===2?[{page:2,attempted:!0,captured:!n,resultCount:e.pageResultCounts.page2,requestOffset:10,providerRequestIdObserved:!0,...n?{failureCode:"empty_page"}:{}}]:[]]);return{seed:e.query,location:null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:{providerConnectMs:e.elapsedMs,totalServerMs:e.elapsedMs},pagination:{requestedPages:t,capturedPages:o,page2Status:t===2?n?"failed":"captured":"not_requested",page1OrganicCount:e.pageResultCounts.page1,page2OrganicCount:e.pageResultCounts.page2},completionStatus:"serp_only",problem:null,resultQuality:n?"partial":"complete",degradedResult:n,degradationReasons:n?["page_two_missing"]:[],retryRecommended:!1,serpCompleteness:{pagination:i,features:{localPack:"not_requested",forums:"not_requested",videos:"not_requested",aiOverview:"not_requested",whatPeopleSaying:"not_requested"},queryIntegrity:ze({query:e.query,finalQuery:e.query,locationResolutionSource:"not_requested",outboundProviderMethod:"default_search"}),failureStage:null}},totalQuestions:0,surface:"web",aiOverview:{detected:!1,text:null,citations:[],shareUrl:null},aiMode:{detected:!1,text:null,citations:[]},whatPeopleSaying:[],tree:[],flat:[],videos:[],forums:[],organicResults:r,localPack:[],entityIds:{entities:[],kgIds:[],cids:[],gcids:[]},stats:{seed:e.query,totalQuestions:0,maxDepthReached:0,durationMs:e.elapsedMs,errorCount:0}}}var sn=12e3;async function Jr(e){let t=await We(e.runId);if(!t||String(t.run.id)!==e.runId||Number(t.run.user_id)!==e.userId||t.run.tool!=="search_serp"||t.run.job_id!==e.jobId||t.attempts.length===0||!t.attempts.every(o=>o.run_id===e.runId&&o.provider==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"&&["\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_native_proxy","\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_serp_rest"].includes(String(o.method))&&["failed","timed_out","cancelled"].includes(String(o.status))&&typeof o.ended_at=="string"&&Number.isFinite(Date.parse(o.ended_at))&&o.duration_ms!=null&&Number.isSafeInteger(Number(o.duration_ms))&&Number(o.duration_ms)>=0))return null;let r=t.attempts[t.attempts.length-1].status;return r==="cancelled"?{errorCode:"request_aborted",httpStatus:408,terminalStatus:"cancelled"}:r==="timed_out"?{errorCode:"harvest_timeout",httpStatus:504,terminalStatus:"failed"}:{errorCode:"service_unavailable",httpStatus:503,terminalStatus:"failed"}}function Gr(e){return e.recency?er():e.mode==="full"&&e.pages===1?Ze():!!e.token||Ze()}async function ht(e){if(e.signal.throwIfAborted(),e.recency)return an(e);if(e.mode==="full")return ln(e);if(!e.token)return $r(e);let t=performance.now(),r;for(let o=1;o<=2;o++){let n=AbortSignal.any([e.signal,AbortSignal.timeout(sn)]),i=e.pages;try{let s=await Fr({options:{query:e.query,pages:i,signal:n},token:e.token,operationRunId:e.operationRunId,accountScope:e.accountScope,attemptNumber:o,...r?{parentAttemptId:r,edgeKind:"same_method_retry",retryKind:"same_method"}:{}});console.info(JSON.stringify({event:"serp_provider_succeeded",provider:"apify",operationRunId:e.operationRunId,attemptNumber:o,durationMs:s.elapsedMs,resultCount:s.results.length,requestedPages:e.pages,providerRequestedPages:s.requestedPages,deliveredPages:s.pageResultCounts.page2>0?2:1,runId:s.runId}));let a=Ur(s,e.pages);return a.stats.durationMs=Math.round(performance.now()-t),a.diagnostics.phaseTimings&&(a.diagnostics.phaseTimings.totalServerMs=a.stats.durationMs),{result:a,mode:"light",deliveryProvider:"apify",deliveredPages:s.pageResultCounts.page2>0?2:1}}catch(s){r=s&&typeof s=="object"&&"attemptId"in s?String(s.attemptId):void 0;let a=s instanceof M?s:null;if(console.warn(JSON.stringify({event:"serp_provider_failed",provider:"apify",operationRunId:e.operationRunId,attemptNumber:o,runId:a?.runId??null,requestedPages:e.pages,attemptPages:i,retrySafe:a?.retrySafe===!0,error:s instanceof Error?s.message:String(s)})),e.signal.aborted||!r||a?.retrySafe!==!0)throw s}}return $r(e,r)}async function $r(e,t){if(e.signal.throwIfAborted(),!Ze())throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 SERP API fallback is not configured; set \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_API_KEY and \u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_SERP_ZONE, or the SERP proxy username and password");let r=performance.now(),o={query:e.query,gl:"us",hl:"en",device:"desktop",pages:1,includeAllSerpFeatures:!1,includeLocalPack:!1,includeForums:!1,includeVideos:!1,includeAiOverview:!1,includeWhatPeopleSaying:!1,signal:e.signal};console.info(JSON.stringify({event:"serp_provider_fallback",provider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",operationRunId:e.operationRunId,attemptNumber:3,requestedPages:e.pages,providerRequestedPages:1}));try{let n=await yt(o,process.env,fetch,void 0,t?{attemptNumber:3,parentAttemptId:t}:void 0);if(n.organicResults.length===0)throw new Error("Search returned no organic results");if(e.pages===2){n.diagnostics.pagination={requestedPages:2,capturedPages:1,page2Status:"unavailable",page1OrganicCount:n.organicResults.length,page2OrganicCount:0},n.diagnostics.resultQuality="partial",n.diagnostics.degradedResult=!0,n.diagnostics.degradationReasons=[...new Set([...n.diagnostics.degradationReasons??[],"page_two_missing"])],n.diagnostics.retryRecommended=!1;let i=n.diagnostics;i.serpCompleteness&&(i.serpCompleteness.pagination=de(2,i.serpCompleteness.pagination.evidence.filter(s=>s.page===1)),i.serpCompleteness.failureStage="pagination")}return n.stats.durationMs=Math.round(performance.now()-r),n.diagnostics.phaseTimings&&(n.diagnostics.phaseTimings.totalServerMs=n.stats.durationMs),console.info(JSON.stringify({event:"serp_provider_succeeded",provider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",operationRunId:e.operationRunId,attemptNumber:3,durationMs:n.stats.durationMs,resultCount:n.organicResults.length,requestedPages:e.pages,providerRequestedPages:1,deliveredPages:1,resultQuality:n.diagnostics.resultQuality})),{result:n,mode:"light",deliveryProvider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",deliveredPages:1}}catch(n){throw console.warn(JSON.stringify({event:"serp_provider_failed",provider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",operationRunId:e.operationRunId,attemptNumber:3,error:n instanceof Error?n.message:String(n)})),n}}async function an(e){let t=await Fe({...e.physicalFactory?{physicalExecution:await e.physicalFactory()}:{},query:e.query,gl:"us",hl:"en",device:"desktop",pages:e.pages,recency:e.recency,serpOnly:!0,forceManagedSerp:!0,maxAttempts:2,proxyMode:"none",maxQuestions:1,depth:1,headless:!0,format:"json",outputDir:"/tmp/paa-output-api",softDeadlineMs:Date.now()+23e4,signal:e.signal,onAttemptEvent:e.onAttemptEvent});if(t.organicResults.length===0)throw new Error("Search returned no organic results");let r=t.diagnostics.pagination?.capturedPages===2?2:1;if(e.pages===2&&r===1&&(t.diagnostics.resultQuality="partial",t.diagnostics.degradedResult=!0,t.diagnostics.degradationReasons=["page_two_missing"],t.diagnostics.retryRecommended=!0),e.mode==="full"){let o=Object.fromEntries(["localPack","forums","videos","aiOverview","aiMode","whatPeopleSaying","entityIds","paaPreview","topStories"].map(n=>[n,"unsupported"]));t.fullSerpFeatures={status:o,paaPreview:[],additional:[],pages:[]}}return{result:t,mode:e.mode==="full"?"full":"light",deliveryProvider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",deliveredPages:r}}async function ln(e){if(e.pages===2){let t=await ht({...e,mode:"light",pages:2}),r=Object.fromEntries(["localPack","forums","videos","aiOverview","aiMode","whatPeopleSaying","entityIds","paaPreview","topStories"].map(n=>[n,"unsupported"]));t.result.fullSerpFeatures={status:r,paaPreview:[],additional:[],pages:[]};let o=t.result.diagnostics;return o.serpCompleteness&&(o.serpCompleteness.features={localPack:"unsupported",forums:"unsupported",videos:"unsupported",aiOverview:"unsupported",whatPeopleSaying:"unsupported"}),{...t,mode:"full"}}return un({...e,mode:"full"})}async function un(e){let t=performance.now(),r=e.mode==="full",o={query:e.query,gl:"us",hl:"en",device:"desktop",pages:1,...e.recency?{recency:e.recency}:{},includeAllSerpFeatures:r,includeLocalPack:r,includeForums:r,includeVideos:r,includeAiOverview:r,includeWhatPeopleSaying:r,signal:e.signal},n=await yt(o);if(n.organicResults.length===0)throw new Error("Search returned no organic results");return n.stats.durationMs=Math.round(performance.now()-t),n.diagnostics.phaseTimings&&(n.diagnostics.phaseTimings.totalServerMs=n.stats.durationMs),{result:n,mode:r?"full":"light",deliveryProvider:"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061",deliveredPages:n.diagnostics.pagination?.capturedPages===2?2:1}}import{randomUUID as so}from"crypto";var Br="2026-10-10-operation-final-settlements.1";async function Wr(e){let t=async()=>{let[r,o,n]=await Promise.all([e.execute("SELECT sql FROM sqlite_master WHERE name='operation_final_settlements'"),e.execute("PRAGMA index_info(operation_final_settlement_key)"),e.execute({sql:"SELECT 1 FROM schema_migrations WHERE version=?",args:[Br]})]),i=String(r.rows[0]?.sql??"").replace(/\s+/g," ");return n.rows.length===1&&o.rows.map(s=>s.name).join(",")==="debit_key"&&i.includes("run_id TEXT NOT NULL REFERENCES operation_runs(id)")&&i.includes("user_id INTEGER NOT NULL CHECK (user_id > 0)")&&i.includes("final_amount_mc INTEGER NOT NULL CHECK (final_amount_mc >= 0)")&&i.includes("receipt_json TEXT NOT NULL CHECK (json_valid(receipt_json))")};if(!await t()){try{await e.batch([`CREATE TABLE IF NOT EXISTS operation_final_settlements (
|
|
49
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
50
|
+
run_id TEXT NOT NULL REFERENCES operation_runs(id),
|
|
51
|
+
user_id INTEGER NOT NULL CHECK (user_id > 0),
|
|
52
|
+
debit_key TEXT NOT NULL REFERENCES operation_debit_plans(debit_key),
|
|
53
|
+
final_amount_mc INTEGER NOT NULL CHECK (final_amount_mc >= 0),
|
|
54
|
+
refund_operation TEXT NOT NULL,source_surface TEXT NOT NULL,
|
|
55
|
+
receipt_json TEXT NOT NULL CHECK (json_valid(receipt_json)),
|
|
56
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
57
|
+
)`,"CREATE UNIQUE INDEX IF NOT EXISTS operation_final_settlement_key ON operation_final_settlements(debit_key)",{sql:"INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)",args:[Br]}],"write")}catch(r){if(!await t().catch(()=>!1))throw r}if(!await t())throw Error("Final settlement schema is incomplete")}}var dn="(CAST(strftime('%s','now') AS INTEGER)*1000)";async function zr(e){let{claim:t,gate:r}=e;if(Ee({userId:t.userId,tool:r.tool,jobId:r.jobId},r),!Number.isSafeInteger(e.amountMc)||e.amountMc<0||!e.key||!e.refundOperation||!e.sourceSurface)throw Error("Invalid final settlement input");let o=h({key:e.key,amountMc:e.amountMc,refundOperation:e.refundOperation,sourceSurface:e.sourceSurface,material:e.material});if(Buffer.byteLength(o)>16*1024*1024)throw Error("Final settlement material exceeds budget");let n=m();await Wr(n),await K({userId:t.userId,runId:t.runId});let i=Ut(t.userId,r.jobId),s="settlement:"+O(`${t.runId}\0${e.key}`),a=`EXISTS(SELECT 1 FROM operation_runs r JOIN operation_executions x ON x.run_id=r.id
|
|
58
|
+
WHERE r.id=? AND r.user_id=? AND r.tool=? AND r.job_id=? AND r.status=? AND x.claim_token=? AND x.phase=? AND x.expires_at_ms>${dn}) AND (${r.sql}) AND (${i.sql})`,l=[t.runId,t.userId,r.tool,r.jobId,e.recovery?"unknown":"running",t.token,e.recovery?"unknown":"effects_started",...r.args,...i.args];try{await n.execute({sql:`INSERT OR IGNORE INTO operation_final_settlements(id,run_id,user_id,debit_key,final_amount_mc,refund_operation,source_surface,receipt_json)
|
|
59
|
+
SELECT ?,?,?,?,?,?,?,? WHERE (${a}) AND EXISTS(SELECT 1 FROM operation_debit_plans p JOIN billing_debits d ON d.idempotency_key=p.debit_key
|
|
60
|
+
WHERE p.run_id=? AND p.debit_key=? AND p.refund_operation=? AND p.source_surface=? AND d.user_id=? AND d.status='applied' AND ?>=0 AND ?<=p.amount_mc)`,args:[s,t.runId,t.userId,e.key,e.amountMc,e.refundOperation,e.sourceSurface,o,...l,t.runId,e.key,e.refundOperation,e.sourceSurface,t.userId,e.amountMc,e.amountMc]})}catch{}let u=(await n.execute({sql:`SELECT s.* FROM operation_final_settlements s WHERE s.id=? AND s.run_id=? AND s.user_id=? AND (${a})`,args:[s,t.runId,t.userId,...l]})).rows[0];if(!u||u.receipt_json!==o)throw Error("final_settlement_plan_unknown_or_conflicting");return{...t,mode:e.recovery?"compensate":"execute",settlementPlanId:s}}function tt(e){return{...e,sql:`EXISTS(SELECT 1 FROM operation_final_settlements s JOIN billing_events b ON b.user_id=s.user_id
|
|
61
|
+
WHERE s.run_id=? AND s.user_id=? AND s.debit_key=? AND b.billing_class='wallet_hold_settlement' AND b.status='settled'
|
|
62
|
+
AND b.source_surface=s.source_surface AND b.amount_mc=s.final_amount_mc AND json_extract(b.metadata,'$.debitIdempotencyKey')=s.debit_key
|
|
63
|
+
AND json_extract(b.metadata,'$.refundOperation')=s.refund_operation)`,args:[e.runId,e.userId,e.key]}}var Vr="2026-10-10-outbound-delivery.1";async function rt(e){let t=async()=>{let r=(await e.execute("SELECT sql FROM sqlite_master WHERE name='outbound_deliveries'")).rows[0],o=(await e.execute({sql:"SELECT 1 FROM schema_migrations WHERE version=?",args:[Vr]})).rows,n=String(r?.sql??"").replace(/\s+/g," "),i=(await e.execute("PRAGMA index_info(outbound_delivery_job)")).rows.map(s=>s.name).join(",");return o.length===1&&i==="job_id,user_id"&&n.includes("request_json TEXT NOT NULL CHECK (json_valid(request_json))")&&n.includes("expires_at_ms INTEGER NOT NULL")&&n.includes("'accepted','unknown','expired','unavailable'")};if(!await t()){try{await e.batch([`CREATE TABLE IF NOT EXISTS outbound_deliveries (
|
|
64
|
+
id TEXT NOT NULL PRIMARY KEY,user_id INTEGER NOT NULL CHECK (user_id > 0),job_id TEXT NOT NULL,
|
|
65
|
+
parent_run_id TEXT NOT NULL REFERENCES operation_runs(id),request_json TEXT NOT NULL CHECK (json_valid(request_json)),
|
|
66
|
+
state TEXT NOT NULL CHECK (state IN ('requested','frozen','sending','accepted','unknown','expired','unavailable')),
|
|
67
|
+
expires_at_ms INTEGER NOT NULL,payload_json TEXT CHECK (payload_json IS NULL OR json_valid(payload_json)),payload_sha256 TEXT,
|
|
68
|
+
send_nonce TEXT,send_token TEXT,http_status INTEGER,receipt_json TEXT CHECK (receipt_json IS NULL OR json_valid(receipt_json)),
|
|
69
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
70
|
+
)`,"CREATE UNIQUE INDEX IF NOT EXISTS outbound_delivery_job ON outbound_deliveries(job_id,user_id)",{sql:"INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)",args:[Vr]}],"write")}catch(r){if(!await t().catch(()=>!1))throw r}if(!await t())throw Error("Delivery schema is incomplete")}}var ee=class extends Error{};var fe="(CAST(strftime('%s','now') AS INTEGER)*1000)",Pe=e=>`delivery:${e}`,te=e=>h(e);async function j(e,t){let r=m();await rt(r);let o=(await r.execute({sql:`SELECT *,${fe} AS now_ms FROM outbound_deliveries WHERE id=? AND user_id=?`,args:[e,t]})).rows[0];if(!o)return null;let n=JSON.parse(String(o.request_json));if(n.id!==e||n.userId!==t||n.jobId!==o.job_id||n.parentRunId!==o.parent_run_id)throw Error("Delivery request conflicts");return{...n,state:String(o.state),expiresAtMs:Number(o.expires_at_ms),nowMs:Number(o.now_ms),payloadJson:o.payload_json==null?null:String(o.payload_json),payloadSha256:o.payload_sha256==null?null:String(o.payload_sha256),sendNonce:o.send_nonce==null?null:String(o.send_nonce),sendToken:o.send_token==null?null:String(o.send_token),httpStatus:o.http_status==null?null:Number(o.http_status),receiptJson:o.receipt_json==null?null:String(o.receipt_json)}}async function Kr(e,t,r){if(Ee({userId:e.userId,tool:r.tool,jobId:e.jobId},r),!e.id||!e.url||!["paa-direct","paa-queue"].includes(e.profile)||!Number.isSafeInteger(e.userId)||e.userId<=0||t.runId!==e.parentRunId||t.userId!==e.userId||!/^[a-f0-9]{64}$/.test(e.fingerprint))throw Error("Invalid delivery request");let o=m();await rt(o);let n;try{await o.execute({sql:`INSERT OR IGNORE INTO outbound_deliveries(id,user_id,job_id,parent_run_id,request_json,state,expires_at_ms)
|
|
71
|
+
SELECT ?,?,?,?,?,'requested',${fe}+? WHERE (${r.sql}) AND EXISTS(SELECT 1 FROM operation_runs r JOIN operation_executions x ON x.run_id=r.id
|
|
72
|
+
WHERE r.id=? AND r.user_id=? AND r.tool=? AND r.job_id=? AND r.status='running' AND x.phase='claimed' AND x.claim_token=? AND x.expires_at_ms>${fe})`,args:[e.id,e.userId,e.jobId,e.parentRunId,te(e),864e5,...r.args,t.runId,t.userId,r.tool,r.jobId,t.token]})}catch(s){n=s}let i=await j(e.id,e.userId);if(!i||te($(i))!==te(e))throw n??Error("Delivery request receipt is unavailable or conflicting");return i}function $(e){return{id:e.id,userId:e.userId,jobId:e.jobId,parentRunId:e.parentRunId,url:e.url,profile:e.profile,fingerprint:e.fingerprint}}async function Xr(e,t,r){let o;try{o=h(t)}catch{throw new ee("Delivery payload cannot be serialized")}let n=O(o);if(Buffer.byteLength(o)>16777216)throw new ee("Delivery payload exceeds budget");Ee({userId:e.userId,tool:r.tool,jobId:e.jobId},r);let i;try{await m().execute({sql:`UPDATE outbound_deliveries SET state='frozen',payload_json=?,payload_sha256=?,updated_at=datetime('now') WHERE id=? AND user_id=? AND request_json=? AND state='requested' AND expires_at_ms>${fe} AND (${r.sql})`,args:[o,n,e.id,e.userId,te($(e)),...r.args]})}catch(a){i=a}let s=await j(e.id,e.userId);if(!s||s.payloadJson!==o||s.payloadSha256!==n)throw i??Error("Delivery payload receipt is unavailable or conflicting");return s}function Oe(e,t,r,o){return{userId:e.userId,tool:"paa_callback",jobId:e.jobId,sql:`(${t.sql}) AND EXISTS(SELECT 1 FROM outbound_deliveries d WHERE d.id=? AND d.user_id=? AND d.request_json=? AND d.state=? AND d.payload_json=? AND d.payload_sha256=? ${r==="accepted"?"":`AND d.expires_at_ms>${fe}`} ${o?"AND d.send_nonce=?":""})`,args:[...t.args,e.id,e.userId,te($(e)),r,e.payloadJson,e.payloadSha256,...o?[o]:[]]}}async function Yr(e,t,r,o){let n=Oe(e,o,"frozen"),i=se(t,{tool:n.tool,jobId:e.jobId}),s;try{await m().execute({sql:`UPDATE outbound_deliveries SET state='sending',send_nonce=?,send_token=?,updated_at=datetime('now') WHERE id=? AND user_id=? AND state='frozen' AND (${n.sql}) AND (${i.sql})`,args:[r,t.token,e.id,e.userId,...n.args,...i.args]})}catch(l){s=l}let a=await j(e.id,e.userId);if(!a||a.state!=="sending"||a.sendNonce!==r||a.sendToken!==t.token||a.payloadSha256!==e.payloadSha256)throw s??Error("Delivery send permission is unavailable");return a}async function Qr(e,t,r){let o=Oe(e,r,"sending",e.sendNonce),n=se(t,{tool:o.tool,jobId:e.jobId});return(await m().execute({sql:`SELECT 1 WHERE (${o.sql}) AND (${n.sql})`,args:[...o.args,...n.args]})).rows.length===1}async function Zr(e,t){if(!e.sendNonce||!e.sendToken||!e.payloadSha256||t<200||t>=300)throw Error("Invalid endpoint acceptance");let r=h({deliveryId:e.id,idempotencyKey:e.id,payloadSha256:e.payloadSha256,httpStatus:t}),o=m(),n;try{await o.execute({sql:`UPDATE outbound_deliveries SET state='accepted',http_status=?,receipt_json=?,updated_at=datetime('now') WHERE id=? AND user_id=? AND state IN ('sending','unknown') AND send_nonce=? AND send_token=? AND payload_sha256=? AND request_json=?
|
|
73
|
+
AND EXISTS(SELECT 1 FROM operation_runs WHERE id=? AND user_id=? AND tool='paa_callback' AND job_id=?)`,args:[t,r,e.id,e.userId,e.sendNonce,e.sendToken,e.payloadSha256,te($(e)),Pe(e.id),e.userId,e.jobId]})}catch(s){n=s}let i=await j(e.id,e.userId);if(!i||i.state!=="accepted"||i.receiptJson!==r)throw n??Error("Endpoint acceptance receipt is unavailable");return i}async function vt(e){await m().execute({sql:"UPDATE outbound_deliveries SET state='unknown',updated_at=datetime('now') WHERE id=? AND user_id=? AND request_json=? AND state='frozen' AND send_nonce IS NULL AND EXISTS(SELECT 1 FROM operation_executions x JOIN operation_runs o ON o.id=x.run_id WHERE o.id=? AND o.user_id=? AND o.tool='paa_callback' AND o.job_id=? AND x.phase IN ('effects_started','unknown'))",args:[e.id,e.userId,te($(e)),Pe(e.id),e.userId,e.jobId]})}async function eo(e){await m().execute({sql:"UPDATE outbound_deliveries SET state='unknown',updated_at=datetime('now') WHERE id=? AND user_id=? AND state='sending' AND send_nonce=?",args:[e.id,e.userId,e.sendNonce]})}async function ot(e,t,r){await m().execute({sql:`UPDATE outbound_deliveries SET state=?,updated_at=datetime('now') WHERE id=? AND user_id=? AND state IN ('requested','frozen') AND send_nonce IS NULL ${t==="expired"?`AND expires_at_ms<=${fe}`:""} ${r?`AND (${r.sql})`:""}`,args:[t,e.id,e.userId,...r?r.args:[]]})}async function L(e){let t=e.state==="sending"?"unknown":e.state;return await m().execute({sql:"UPDATE jobs SET options=json_set(options,'$.callbackDeliveryState',?,'$.callbackDeliveryId',?,'$.callbackDeliveryExpiresAtMs',?) WHERE id=? AND user_id=? AND callback_url=? AND json_extract(options,'$.callbackDeliveryProtocol')=1",args:[t,e.id,e.expiresAtMs,e.jobId,e.userId,e.url]}).catch(()=>{}),{state:t,deliveryId:e.id,expiresAtMs:e.expiresAtMs,...e.httpStatus?{httpStatus:e.httpStatus}:{}}}async function to(e=25){let t=m();return await rt(t),(await t.execute({sql:"SELECT d.id,d.user_id,d.job_id FROM outbound_deliveries d JOIN jobs j ON j.id=d.job_id AND j.user_id=d.user_id WHERE (d.state IN ('requested','frozen') OR (d.state='accepted' AND EXISTS(SELECT 1 FROM operation_executions x WHERE x.run_id='delivery:'||d.id AND x.phase<>'published'))) AND j.status IN ('done','failed','cancelled') AND json_extract(j.options,'$.executionState')='finalized' ORDER BY d.created_at LIMIT ?",args:[Math.max(1,Math.min(100,Math.floor(e)))]})).rows.map(r=>({id:String(r.id),userId:Number(r.user_id),jobId:String(r.job_id)}))}function wt(e){if(!e.callback_url||e.options.callbackDeliveryProtocol!==1)return null;let t=e.options.executionProtocol===1&&e.options.directCompletionProtocol===1&&e.options.executionOwner==="direct",r=e.options.queueExecutionProtocol===1&&e.options.executionOwner==="cron";if(!t&&!r)throw Error("Callback parent profile is unavailable");return{id:`paa-callback:${e.id}`,userId:e.user_id,jobId:e.id,parentRunId:t?`paa-execution:${e.id}`:`harvest:${e.id}`,url:e.callback_url,profile:t?"paa-direct":"paa-queue",fingerprint:String(e.options.requestFingerprint)}}async function ro(e,t,r){let o=wt(e);o&&await Kr(o,t,{...r,sql:`(${r.sql}) AND EXISTS(SELECT 1 FROM jobs WHERE id=? AND user_id=? AND callback_url=? AND json_extract(options,'$.callbackDeliveryProtocol')=1 AND json_extract(options,'$.requestFingerprint')=?)`,args:[...r.args,e.id,e.user_id,e.callback_url,o.fingerprint]})}function oo(e,t){let r=wt(e);return r?{...t,sql:`(${t.sql}) AND EXISTS(SELECT 1 FROM outbound_deliveries WHERE id=? AND user_id=? AND request_json=? AND state='requested')`,args:[...t.args,r.id,r.userId,h(r)]}:t}async function no(e,t){let r=await S(e,t);if(!r?.callback_url)return{kind:"none"};let o=wt(r);if(!o)return{kind:"unavailable"};let n=await j(o.id,r.user_id);if(!n||h($(n))!==h(o))return{kind:"unavailable"};if(!["done","failed","cancelled"].includes(r.status)||r.options.executionState!=="finalized"||r.options.holdState!=="settled"||!r.options.syncCapturedAt)return{kind:"pending",record:n};let i=await T(o.parentRunId,r.user_id,o.profile==="paa-direct"?"harvest_paa":"search_serp",r.id);if(!i||i.phase!=="published")return{kind:"pending",record:n};let s=o.profile==="paa-direct"?i.flags:i.flags.intent;if(!s||s.callbackUrl!==o.url||s.requestFingerprint!==o.fingerprint||s.userId!==r.user_id||s.jobId!==r.id||r.query!==s.options?.query||h(Object.fromEntries(Object.keys(s.options).map(p=>[p,r.options[p]])))!==h(s.options)||s.hold?.userId!==r.user_id||s.hold.key!==r.options.billingDebitKey||s.hold.amountMc!==r.options.billingHoldMc||r.status!==r.options.syncTerminalStatus||r.options.syncFinalAmountMc!==r.options.billingFinalAmountMc)return{kind:"unavailable"};if(r.options.syncRequiresPhysicalReceipt===1&&(await oe(o.parentRunId,r.user_id)).length===0)return{kind:"pending",record:n};if(await K({runId:o.parentRunId,userId:r.user_id}))return{kind:"pending",record:n};let a=O(h({result:r.result,error:r.publicError,httpStatus:r.options.syncHttpStatus,requiresPhysical:r.options.syncRequiresPhysicalReceipt})),l={jobId:r.id,requestFingerprint:s.requestFingerprint,captureSha256:a,status:r.status},u=h({...l,amountMc:Number(r.options.syncFinalAmountMc),physicalOutcome:"terminated"}),d=tt({userId:r.user_id,jobId:r.id,tool:o.profile==="paa-direct"?"harvest_paa":"search_serp",runId:o.parentRunId,key:s.hold.key}),c={...d,sql:`(${d.sql}) AND EXISTS(SELECT 1 FROM jobs j JOIN operation_runs r ON r.job_id=j.id AND r.user_id=j.user_id JOIN operation_executions x ON x.run_id=r.id JOIN operation_final_settlements s ON s.run_id=r.id AND s.user_id=r.user_id
|
|
74
|
+
WHERE j.id=? AND j.user_id=? AND r.id=? AND x.phase='published' AND r.status=x.result_status AND x.result_json=? AND j.status=?
|
|
75
|
+
AND j.callback_url=? AND json_extract(j.options,'$.requestFingerprint')=? AND r.request_fingerprint=? AND j.query=json_extract(r.normalized_flags_json,?)
|
|
76
|
+
AND NOT EXISTS(SELECT 1 FROM json_each(r.normalized_flags_json,?) v WHERE NOT EXISTS(SELECT 1 FROM json_each(j.options) actual WHERE actual.key=v.key AND actual.type=v.type AND actual.value IS v.value))
|
|
77
|
+
AND json_extract(j.options,'$.callbackDeliveryProtocol')=1 AND json_extract(j.options,'$.executionState')='finalized' AND json_extract(j.options,'$.holdState')='settled'
|
|
78
|
+
AND json_extract(j.options,'$.syncFinalAmountMc')=? AND json_extract(j.options,'$.billingFinalAmountMc')=? AND json_extract(j.options,'$.syncTerminalStatus')=? AND json(j.result) IS json(?) AND j.public_error_json IS ?
|
|
79
|
+
AND s.debit_key=? AND s.final_amount_mc=? AND json_extract(s.receipt_json,'$.material.captureSha256')=?)`,args:[...d.args,r.id,r.user_id,o.parentRunId,u,r.status,o.url,o.fingerprint,i.fingerprint,o.profile==="paa-direct"?"$.options.query":"$.intent.options.query",o.profile==="paa-direct"?"$.options":"$.intent.options",Number(r.options.syncFinalAmountMc),Number(r.options.syncFinalAmountMc),r.status,r.result==null?null:h(r.result),r.publicError==null?null:JSON.stringify(r.publicError),s.hold.key,Number(r.options.syncFinalAmountMc),a]};return{kind:"ready",job:r,record:n,domain:c}}var z=e=>`harvest:${e}`,io=e=>e.profile==="direct"?"executionProtocol":e.profile==="queue"?"queueExecutionProtocol":"syncExecutionProtocol";function _e(e,t="'running'"){let r=h(e.options),o=Object.keys(JSON.parse(r)).sort();if(o.some(n=>!/^[a-zA-Z][a-zA-Z0-9]*$/.test(n)))throw Error("Invalid synchronous option key");return{userId:e.userId,tool:e.tool,jobId:e.jobId,sql:`EXISTS(SELECT 1 FROM jobs j WHERE j.id=? AND j.user_id=? AND j.status IN (${t}) AND json_extract(j.options,'$.${io(e)}')=1 AND j.query=? AND j.callback_url IS ? ${e.profile==="direct"?"AND json_extract(j.options,'$.executionOwner')='direct' AND json_extract(j.options,'$.directCompletionProtocol')=1 AND json_extract(j.options,'$.paaLeaseToken')=? AND json_extract(j.options,'$.executionRunId')=?":e.profile==="queue"?"AND json_extract(j.options,'$.executionOwner')='cron'":""} AND json_extract(j.options,'$.requestFingerprint')=? AND json_extract(j.options,'$.billingHoldMc')=? ${o.map(()=>"AND json_extract(j.options,?) IS json_extract(?,?)").join(" ")})`,args:[e.jobId,e.userId,String(e.options.query),e.callbackUrl??null,...e.profile==="direct"?[e.recordToken,`paa-execution:${e.jobId}`]:[],e.requestFingerprint,e.hold.amountMc,...o.flatMap(n=>[`$.${n}`,r,`$.${n}`])]}}function Et(e){let t=JSON.parse(h(e));if(!t.jobId||!Number.isSafeInteger(t.userId)||t.userId<=0||!["harvest_paa","search_serp"].includes(t.tool)||!Number.isSafeInteger(t.deadlineAt)||!Number.isSafeInteger(t.hold.amountMc)||t.hold.amountMc<=0||t.hold.userId!==t.userId||t.hold.key!==(t.profile==="queue"?`serp-search:${t.jobId}:hold`:`harvest-sync:${t.jobId}:charge`)||!/^[a-f0-9]{64}$/.test(t.requestFingerprint)||!t.refundOperation||!t.sourceSurface)throw Error("Invalid synchronous funded intent");if(t.profile==="queue"&&(t.tool!=="search_serp"||t.options.serpOnly!==!0||t.deadlineAt!==0||!Number.isSafeInteger(t.budgetMs)||t.budgetMs<1e3||t.budgetMs>36e5||t.hold.operation!=="serp"||t.refundOperation!=="serp_refund"||t.sourceSurface!=="serp_search"||t.hold.description!==`SERP search: ${t.options.query}`.slice(0,500)))throw Error("Invalid queued compatibility intent");return t}async function ao(e){let t=await T(z(e.id),e.user_id,e.options.serpOnly===!0?"search_serp":"harvest_paa",e.id);if(!t||t.flags.syncExecutionProtocol!==1||!t.flags.intent)return null;let r=Et(t.flags.intent);if((r.profile==="queue"?e.options.queueExecutionProtocol:e.options.syncExecutionProtocol)!==1||r.jobId!==e.id||r.userId!==e.user_id||t.fingerprint!==r.requestFingerprint)throw Error("Synchronous intent receipt conflicts");return r}async function W(e,t="held"){let r=e.options.serpOnly===!0?2:mt;return{status:503,headers:{"Retry-After":String(r)},body:{job_id:e.id,status:"running",saved_status:e.status,...B({errorCode:"response_lost",retryable:!0,chargeStatus:t==="held"?"refund_pending":void 0,details:{retry_guidance:`Wait at least ${r} seconds before retrying this exact request with the same idempotencyKey or reading the returned job. Saved content and settlement resume without another acquisition.`}}),executionState:"unknown",hold_state:t,status_tool:e.options.queueExecutionProtocol===1?"harvest_paa_status":e.options.serpOnly===!0?"search_serp":"harvest_paa_status",...e.result?{result:e.result}:{}}}}async function mn(e,t){await m().execute({sql:`UPDATE jobs SET options=json_set(options,'$.executionState','unknown','$.holdState',CASE WHEN json_extract(options,'$.billingSettledAt') IS NOT NULL THEN 'settled' ELSE 'held' END) WHERE id=? AND user_id=? AND json_extract(options,'$.${io(e)}')=1 AND EXISTS(SELECT 1 FROM operation_executions x WHERE x.run_id=? AND x.claim_token=?)`,args:[e.jobId,e.userId,t.runId,t.token]})}async function nt(e,t,r,o,n,i=null,s=200,a=!0){if(!Number.isSafeInteger(o)||o<0||o>e.hold.amountMc)throw Error("Invalid captured synchronous charge");let l=r==null?null:h(r),u=i==null?null:JSON.stringify(i),d=_e(e),c;try{await m().execute({sql:`UPDATE jobs SET result=?,public_error_json=?,options=json_set(options,'$.syncCapturedAt',datetime('now'),'$.syncFinalAmountMc',?,'$.syncTerminalStatus',?,'$.syncHttpStatus',?,'$.syncRequiresPhysicalReceipt',?)
|
|
80
|
+
WHERE id=? AND user_id=? AND json_extract(options,'$.syncCapturedAt') IS NULL AND (${d.sql})
|
|
81
|
+
AND EXISTS(SELECT 1 FROM operation_runs r JOIN operation_executions x ON x.run_id=r.id WHERE r.id=? AND r.user_id=? AND x.claim_token=? AND x.phase IN ('effects_started','unknown') AND r.status IN ('running','unknown'))`,args:[l,u,o,n,s,a?1:0,e.jobId,e.userId,...d.args,t.runId,t.userId,t.token]})}catch(b){c=b}let p=await S(e.jobId,e.userId);if(!p?.options.syncCapturedAt||h({result:p.result})!==h({result:r==null?null:JSON.parse(l)})||p.options.syncFinalAmountMc!==o||p.options.syncTerminalStatus!==n||h({error:p.publicError})!==h({error:Wt(i)})||p.options.syncHttpStatus!==s||p.options.syncRequiresPhysicalReceipt!==(a?1:0))throw c??Error("synchronous_capture_receipt_unknown_or_conflicting");return p}async function it(e,t){return{status:t?200:Number(e.options.syncHttpStatus??200),body:{job_id:e.id,status:e.status,...e.result?{result:e.result}:{},...e.publicError?{...e.publicError,error:e.publicError.message,...e.options.holdState==="settled"&&e.options.billingFinalAmountMc===0?{charge_status:"refunded"}:{}}:{},...t?{replayed:!0}:{}}}}async function lo(e,t){if(e.profile!==void 0||e.tool!=="search_serp"||e.options.serpOnly!==!0||e.options.mode!=="full"||e.options.pages!==1||e.options.recency||e.options.serpIdentity||t.runId!==z(e.jobId)||t.userId!==e.userId||await K({userId:t.userId,runId:t.runId})||(await oe(t.runId,t.userId)).length!==0)return null;let r=await Jr({runId:t.runId,userId:t.userId,jobId:e.jobId});return r?{publicError:B({errorCode:r.errorCode,retryable:r.terminalStatus!=="cancelled"}),httpStatus:r.httpStatus,terminalStatus:r.terminalStatus}:null}async function st(e,t,r,o=!1){let n=await S(e.jobId,e.userId);if(!n?.options.syncCapturedAt)throw Error("Synchronous capture is not known");if(n.options.syncRequiresPhysicalReceipt===1&&(await oe(t.runId,t.userId)).length===0)throw Error("synchronous_physical_receipt_missing");if(await K({userId:e.userId,runId:t.runId}))throw Error("synchronous_physical_outcome_unknown");let i=Number(n.options.syncFinalAmountMc),s=String(n.options.syncTerminalStatus);if(!["done","failed","cancelled"].includes(s))throw Error("Invalid synchronous terminal receipt");let a={jobId:n.id,requestFingerprint:e.requestFingerprint,captureSha256:O(h({result:n.result,error:n.publicError,httpStatus:n.options.syncHttpStatus,requiresPhysical:n.options.syncRequiresPhysicalReceipt})),status:s},l=_e(e,"'running','done','failed','cancelled'"),u={...l,sql:`(${l.sql}) AND EXISTS(SELECT 1 FROM jobs WHERE id=? AND user_id=? AND json_extract(options,'$.syncCapturedAt') IS NOT NULL AND json_extract(options,'$.syncFinalAmountMc')=? AND json_extract(options,'$.syncTerminalStatus')=? AND json(result) IS json(?) AND public_error_json IS ? AND json_extract(options,'$.syncHttpStatus')=? AND json_extract(options,'$.syncRequiresPhysicalReceipt')=?)`,args:[...l.args,n.id,n.user_id,i,s,n.result==null?null:h(n.result),n.publicError==null?null:JSON.stringify(n.publicError),Number(n.options.syncHttpStatus),Number(n.options.syncRequiresPhysicalReceipt)]},d=await zr({claim:t,recovery:r,gate:u,key:e.hold.key,amountMc:i,refundOperation:e.refundOperation,sourceSurface:e.sourceSurface,material:a});if((await cr(e.userId,e.hold.key,e.sourceSurface)??await ur(e.userId,e.hold.key,i,e.refundOperation,"Synchronous harvest settlement",e.sourceSurface,d)).final_amount_mc!==i)throw Error("Synchronous settlement receipt conflicts");let p=tt({userId:n.user_id,tool:e.tool,jobId:n.id,runId:t.runId,key:e.hold.key}),b=se(t,{tool:e.tool,jobId:n.id,recovery:r}),v;try{await m().execute({sql:`UPDATE jobs SET status=?,completed_at=COALESCE(completed_at,datetime('now')),options=json_set(options,'$.executionState','finalized','$.holdState','settled','$.physicalOutcome','terminated','$.billingSettledAt',COALESCE(json_extract(options,'$.billingSettledAt'),datetime('now')),'$.billingFinalAmountMc',?${e.profile==="direct"?",'$.billedMc',?":""}) WHERE id=? AND user_id=? AND (${u.sql}) AND (${b.sql}) AND (${p.sql})`,args:[s,i,...e.profile==="direct"?[i]:[],n.id,n.user_id,...u.args,...b.args,...p.args]})}catch(H){v=H}let _=await S(n.id,n.user_id);if(!_||_.status!==s||_.options.billingFinalAmountMc!==i||_.options.holdState!=="settled"||h({result:_.result})!==h({result:n.result}))throw v??Error("synchronous_publication_receipt_unknown");let w={status:r?"unknown":s==="done"?"succeeded":s,json:h({...a,amountMc:i,physicalOutcome:"terminated"})};if(!(r?await Je(t,w,p):await $e(t,w)))throw Error("Synchronous execution receipt publication unknown");return it(_,o)}async function St(e){if(e.options.syncExecutionProtocol!==1&&e.options.queueExecutionProtocol!==1)return null;let t=await ao(e);if(!t)return e.options.queueExecutionProtocol===1||await Ge(z(e.id),e.user_id)?W(e,"unconfirmed"):null;let r=await T(z(e.id),e.user_id,t.tool,e.id);if(r?.phase==="published"&&e.options.holdState==="settled")return it(e,!0);if(r?.phase==="claimed"&&r.expiresAtMs<=r.nowMs&&e.status==="running"&&!e.options.syncCapturedAt)return null;if(r?.phase==="claimed"||r?.phase==="effects_started"&&r.expiresAtMs>r.nowMs)return{status:409,headers:{"Retry-After":String(t.tool==="harvest_paa"?mt:2)},body:{job_id:e.id,error:"This request is already running under the supplied Idempotency-Key.",errorCode:"idempotency_in_progress",retryable:!0,...e.result?{result:e.result}:{}}};let o=await Ue(z(e.id),e.user_id);if(!o)return W(e);try{if(!e.options.syncCapturedAt){let n=await lo(t,o);n&&await nt(t,o,null,0,n.terminalStatus,n.publicError,n.httpStatus,!1)}return await st(t,o,!0,!0)}catch{return W(await S(e.id,e.user_id)??e,e.options.holdState==="settled"?"settled":"held")}}async function uo(e,t,r=!0){let o=await pt(t.userId,t.hold.key);if(!o||o.status!=="applied"){if(!r)throw Error("Original applied hold is unavailable");let n=await lr(t.userId,t.hold.amountMc,t.hold.operation,t.hold.description,t.hold.key);if(!n.ok)return{status:402,body:{job_id:t.jobId,error:"Insufficient balance",error_code:"insufficient_balance",balance_mc:n.balance_mc,required_mc:t.hold.amountMc}}}return await dr(e,{hold:t.hold,label:t.profile==="queue"?"original-queue-hold":"original-sync-hold",refundOperation:t.refundOperation,sourceSurface:t.sourceSurface,tool:t.tool,jobId:t.jobId}),null}async function co(e,t,r){let o=Et(e),n,i=!1,s=!1,a=!1,l=await S(o.jobId,o.userId);try{let u=await ao(l);if(u){if(u.requestFingerprint!==o.requestFingerprint)throw Error("Synchronous request conflicts");o=u}else if(o.profile==="queue"||await Ge(z(o.jobId),o.userId)||(await pt(o.userId,o.hold.key))?.status==="applied")return W(l,"unconfirmed");if(o.profile!=="queue"&&Date.now()>=o.deadlineAt)return W(l,"unconfirmed");let d=await Ae({id:z(o.jobId),userId:o.userId,ownerScope:`user:${o.userId}`,tool:o.tool,jobId:o.jobId,idempotencyKey:`job:${o.jobId}`,requestFingerprint:o.requestFingerprint,normalizedFlags:{syncExecutionProtocol:1,intent:o}},so(),Math.min(36e5,Math.max(1e3,(o.profile==="queue"?o.budgetMs:o.deadlineAt-Date.now())+3e4)),_e(o));if(d.kind!=="claimed")return W(l,"unconfirmed");n=d.claim;let c=await uo(n,o);if(c)return c;let p=_e(o),b=ir(o.hold),v=await S(o.jobId,o.userId),_=oo(v,{...p,sql:`(${p.sql}) AND (${b.sql})`,args:[...p.args,...b.args]});if(!await Be(n,_))throw Error("Synchronous effect start unknown");if(i=!0,o.profile==="queue"){let Ce=se(n,{tool:o.tool,jobId:o.jobId}),I=_e(o),je;try{await m().execute({sql:`UPDATE jobs SET options=json_set(options,'$.compatDeadlineAt',CAST(strftime('%s','now') AS INTEGER)*1000+?) WHERE id=? AND user_id=? AND json_extract(options,'$.compatDeadlineAt') IS NULL AND (${I.sql}) AND (${Ce.sql})`,args:[o.budgetMs,o.jobId,o.userId,...I.args,...Ce.args]})}catch(ye){je=ye}let qe=await S(o.jobId,o.userId),V=Number(qe?.options.compatDeadlineAt);if(!Number.isSafeInteger(V)||V<=Date.now())throw je??Error("Queued deadline unavailable or expired");o={...o,deadlineAt:V}}let w=0,k=n,H={claim:n,deadlineAt:o.deadlineAt,physicalFactory:async()=>Ft({claim:k,gate:_,deadlineAt:o.deadlineAt,scope:`sync-browser-${++w}`,profile:"customer-funded"})},J=await t(H);a=!0,await nt(o,n,J.result,J.amountMc,"done",null,200,o.options.serpOnly!==!0||w>0);let at=await st(o,n,!1);return s=!0,at}catch(u){if(n&&i){if(!(await S(o.jobId,o.userId))?.options.syncCapturedAt&&!a){let c=await oe(n.runId,n.userId).catch(()=>[]);if(c.length>0&&c.every(p=>p.state==="terminated"||p.state==="not_started")){let p=r(u);try{await nt(o,n,null,0,p.terminalStatus==="cancelled"?"cancelled":"failed",p.publicError,p.httpStatus);let b=await st(o,n,!1);return s=!0,b}catch{}}if(c.length===0){let p=await lo(o,n).catch(()=>null);if(p)try{await nt(o,n,null,0,p.terminalStatus,p.publicError,p.httpStatus,!1);let b=await st(o,n,!1);return s=!0,b}catch{}}}await ne(n).catch(()=>{}),await mn(o,n).catch(()=>{})}return W(await S(o.jobId,o.userId)??l)}finally{n&&!i&&await Se(n).catch(()=>{}),n&&i&&!s&&await ne(n).catch(()=>{})}}async function At(e,t=!0){let r=Et(e);if(r.profile!=="queue")throw Error("Queue profile required");let o=!1,n=z(r.jobId),i=await S(r.jobId,r.userId),s=async()=>({kind:"unknown",reply:i?await W(i):{status:503,body:{job_id:r.jobId,status:"pending",executionState:"unknown",hold_state:o?"held":"unconfirmed",status_tool:"harvest_paa_status",...B({errorCode:"response_lost",retryable:!0})}}});if(i&&i.options.queueExecutionProtocol!==1)return s();let a=await T(n,r.userId,r.tool,r.jobId);if(a){if(a.flags.syncExecutionProtocol!==1||h({intent:a.flags.intent})!==h({intent:r}))return s();if(["effects_started","unknown","published"].includes(a.phase)){if(!i)return s();let d=await St(i);if(d&&d.status!==200)return{kind:"unknown",reply:d};let c=await S(r.jobId,r.userId);return c?{kind:"ready",job:c,replayed:!0}:s()}}else if(i||await Ge(n,r.userId))return s();let l={userId:r.userId,tool:r.tool,jobId:r.jobId,sql:"EXISTS(SELECT 1 FROM users WHERE id=? AND active=1)",args:[r.userId]},u;try{let d=await Ae({id:n,userId:r.userId,ownerScope:`user:${r.userId}`,tool:r.tool,jobId:r.jobId,idempotencyKey:`job:${r.jobId}`,requestFingerprint:r.requestFingerprint,normalizedFlags:{syncExecutionProtocol:1,intent:r}},so(),45e3,l);if(d.kind!=="claimed")return s();u=d.claim;let c=await uo(u,r,t);if(c)return{kind:"unfunded",reply:c};if(o=!0,!i&&(await yr(r.jobId,r.userId,String(r.options.query),{...r.options,executionOwner:"cron",queueExecutionProtocol:1,...r.callbackUrl?{callbackDeliveryProtocol:1}:{},billingHoldMc:r.hold.amountMc,billingDebitKey:r.hold.key,requestFingerprint:r.requestFingerprint},r.callbackUrl??void 0)).kind==="unknown")return s();let p=_e(r,"'pending','running','done','failed','cancelled'"),b=(await m().execute({sql:`SELECT 1 WHERE (${p.sql})`,args:p.args})).rows.length>0,v=await S(r.jobId,r.userId);return b&&v&&await ro(v,u,p),b&&v?{kind:"ready",job:v,replayed:!!(i||a)}:s()}catch{return s()}finally{u&&await Se(u).catch(()=>{})}}function po(e){let t=JSON.parse(h(e.options)),r=t.serpOnly===!0&&!t.serpIdentity;return{profile:"queue",jobId:e.jobId,userId:e.userId,tool:"search_serp",requestFingerprint:e.requestFingerprint,options:t,callbackUrl:e.callbackUrl??null,deadlineAt:0,budgetMs:Jt(Number(t.maxQuestions??30),!0,t.mode,t.pages,!!t.recency,r).serverMs,hold:{userId:e.userId,key:`serp-search:${e.jobId}:hold`,amountMc:e.amountMc,operation:"serp",description:`SERP search: ${t.query}`.slice(0,500)},refundOperation:"serp_refund",sourceSurface:"serp_search"}}var hi=e=>At(po(e));async function gn(e,t){let r;try{r=await T(`harvest:${t}`,e,"search_serp",t)}catch{return{kind:"unknown",reply:{status:503,body:{job_id:t,status:"pending",executionState:"unknown",hold_state:"unconfirmed",status_tool:"harvest_paa_status",...B({errorCode:"response_lost",retryable:!0})}}}}return r?.flags.syncExecutionProtocol!==1||r.flags.intent?.profile!=="queue"?null:At(r.flags.intent,!1)}async function mo(){for(let t of await hr("search_serp",1,`json_extract(r.normalized_flags_json,'$.intent.profile')='queue' AND (EXISTS(SELECT 1 FROM (SELECT r.user_id AS user_id,json_extract(r.normalized_flags_json,'$.intent') AS normalized_flags_json) saved WHERE ${ar("saved.user_id","json_extract(saved.normalized_flags_json,'$.hold.key')")}))`))await S(t.jobId,t.userId)||await gn(t.userId,t.jobId);let e=(await m().execute(`SELECT j.id,j.user_id FROM jobs j JOIN operation_runs r ON r.id='harvest:'||j.id AND r.user_id=j.user_id JOIN operation_executions x ON x.run_id=r.id
|
|
82
|
+
WHERE json_extract(j.options,'$.queueExecutionProtocol')=1 AND x.phase IN ('effects_started','unknown') AND x.expires_at_ms<=CAST(strftime('%s','now') AS INTEGER)*1000
|
|
83
|
+
ORDER BY j.created_at LIMIT 1`)).rows[0];return e?S(String(e.id),Number(e.user_id)):void 0}async function go(e,t){if(e.options.queueExecutionProtocol!==1)return await m().execute({sql:"UPDATE jobs SET options=json_set(options,'$.queueExecutionProtocol',0,'$.executionState','unknown','$.holdState','unconfirmed') WHERE id=? AND user_id=? AND json_extract(options,'$.queueExecutionProtocol') IS NULL",args:[e.id,e.user_id]}),{completed:!1,replayed:!1,reply:null};let r=await St(e);if(r)return{completed:(await S(e.id,e.user_id))?.options.executionState==="finalized"&&r.status===200,replayed:!0,reply:r};let o=e.options,n=Object.fromEntries(Object.entries(o).filter(([l])=>!["executionOwner","queueExecutionProtocol","billingHoldMc","billingDebitKey","requestFingerprint","compatDeadlineAt","executionState","holdState"].includes(l))),i=po({jobId:e.id,userId:e.user_id,options:n,requestFingerprint:String(o.requestFingerprint),amountMc:Number(o.billingHoldMc),callbackUrl:e.callback_url}),s=await co(i,async l=>{let u={value:null},d=o.serpOnly===!0&&!o.serpIdentity,c=`harvest:${e.id}`,p=null,b=AbortSignal.timeout(Math.max(0,l.deadlineAt-Date.now()));if(d&&!Gr({pages:o.pages===2?2:1,recency:o.recency==="week"||o.recency==="month"?o.recency:void 0,mode:o.mode,token:t.apifyToken}))throw Error("Search providers are not configured");let v=await t.withCostContext({op:"serp",userId:e.user_id,headlessSentOut:u,operationRunId:c,operationAccountScope:"production-default"},async()=>d?ht({query:String(o.query??e.query),pages:o.pages===2?2:1,mode:o.mode,recency:o.recency==="week"||o.recency==="month"?o.recency:void 0,token:t.apifyToken,operationRunId:c,accountScope:"production-default",signal:b,onAttemptEvent:t.onAttemptEvent,physicalFactory:l.physicalFactory}).then(k=>(p=k,k.result)):Fe({...o,physicalExecution:await l.physicalFactory(),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,headless:!0,format:"json",outputDir:"/tmp/paa-output-api",signal:b,onAttemptEvent:t.onAttemptEvent}));if(d&&!p)throw Error("Search provider billing receipt is missing");let _=p,w=d?mr(_.deliveredPages,_.deliveryProvider):pr(u.value);return{result:v,amountMc:Math.min(Number(o.billingHoldMc),w)}},t.classifyFailure);return{completed:(await S(e.id,e.user_id))?.options.executionState==="finalized"&&!!(s.body.status&&["done","failed","cancelled"].includes(String(s.body.status))),replayed:!1,reply:s}}import{randomUUID as fn}from"crypto";function Te(e){return e==null?null:Number(e)===1}function fo(e){if(e==null)return null;try{return JSON.parse(String(e))}catch{return null}}function _o(e){return{id:String(e.id),job_id:String(e.job_id),user_id:Number(e.user_id),attempt_number:Number(e.attempt_number),max_attempts:Number(e.max_attempts),query:String(e.query),location:e.location!=null?String(e.location):null,max_questions:Number(e.max_questions),status:String(e.status),outcome:e.outcome!=null?String(e.outcome):null,\u006b\u0065\u0072\u006e\u0065\u006c_session_id:e.\u006b\u0065\u0072\u006e\u0065\u006c_session_id!=null?String(e.\u006b\u0065\u0072\u006e\u0065\u006c_session_id):null,question_count:e.question_count!=null?Number(e.question_count):null,duration_ms:e.duration_ms!=null?Number(e.duration_ms):null,error:e.error!=null?String(e.error):null,will_retry:Te(e.will_retry),\u006b\u0065\u0072\u006e\u0065\u006c_delete_started:Number(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_started??0)===1,\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded:Te(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded),\u006b\u0065\u0072\u006e\u0065\u006c_delete_error:e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_error!=null?String(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_error):null,browser_close_succeeded:Te(e.browser_close_succeeded),browser_close_error:e.browser_close_error!=null?String(e.browser_close_error):null,browser_provider:e.browser_provider!=null?String(e.browser_provider):null,provider_session_id:e.provider_session_id!=null?String(e.provider_session_id):null,provider_session_observed_at:e.provider_session_observed_at!=null?String(e.provider_session_observed_at):null,provider_lookup_status:e.provider_lookup_status!=null?String(e.provider_lookup_status):null,provider_status:e.provider_status!=null?String(e.provider_status):null,provider_error_code:e.provider_error_code!=null?String(e.provider_error_code):null,provider_error_message:e.provider_error_message!=null?String(e.provider_error_message):null,provider_reason_source:e.provider_reason_source!=null?String(e.provider_reason_source):null,provider_duration_ms:e.provider_duration_ms!=null?Number(e.provider_duration_ms):null,provider_bandwidth_bytes:e.provider_bandwidth_bytes!=null?Number(e.provider_bandwidth_bytes):null,provider_captcha:Te(e.provider_captcha),provider_checked_at:e.provider_checked_at!=null?String(e.provider_checked_at):null,provider_detail:fo(e.provider_detail_json),provider_disconnect_observed:Te(e.provider_disconnect_observed),provider_disconnect_message:e.provider_disconnect_message!=null?String(e.provider_disconnect_message):null,debug:fo(e.debug_json),created_at:String(e.created_at),completed_at:e.completed_at!=null?String(e.completed_at):null}}async function yo(e){await m().execute({sql:`
|
|
84
|
+
INSERT INTO harvest_attempts (
|
|
85
|
+
id, job_id, user_id, attempt_number, max_attempts, query, location, max_questions, status, created_at
|
|
86
|
+
)
|
|
87
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'running', ?)
|
|
88
|
+
ON CONFLICT(job_id, attempt_number) DO NOTHING
|
|
89
|
+
`,args:[fn(),e.jobId,e.userId,e.attemptNumber,e.maxAttempts,e.query,e.location,e.maxQuestions,e.startedAt]})}async function bo(e){return(await m().execute({sql:`
|
|
90
|
+
UPDATE harvest_attempts SET
|
|
91
|
+
browser_provider = ?,
|
|
92
|
+
provider_session_id = ?,
|
|
93
|
+
provider_session_observed_at = COALESCE(provider_session_observed_at, ?),
|
|
94
|
+
provider_lookup_status = CASE
|
|
95
|
+
WHEN provider_lookup_status = 'complete' THEN provider_lookup_status
|
|
96
|
+
ELSE 'pending'
|
|
97
|
+
END
|
|
98
|
+
WHERE job_id = ? AND attempt_number = ? AND status = 'running'
|
|
99
|
+
`,args:[e.browserProvider,e.providerSessionId,e.observedAt,e.jobId,e.attemptNumber]})).rowsAffected===1}async function ho(e){await m().execute({sql:`
|
|
100
|
+
UPDATE harvest_attempts SET
|
|
101
|
+
status = 'finished',
|
|
102
|
+
outcome = ?,
|
|
103
|
+
\u006b\u0065\u0072\u006e\u0065\u006c_session_id = ?,
|
|
104
|
+
question_count = ?,
|
|
105
|
+
duration_ms = ?,
|
|
106
|
+
error = ?,
|
|
107
|
+
will_retry = ?,
|
|
108
|
+
\u006b\u0065\u0072\u006e\u0065\u006c_delete_started = ?,
|
|
109
|
+
\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded = ?,
|
|
110
|
+
\u006b\u0065\u0072\u006e\u0065\u006c_delete_error = ?,
|
|
111
|
+
browser_close_succeeded = ?,
|
|
112
|
+
browser_close_error = ?,
|
|
113
|
+
browser_provider = COALESCE(browser_provider, ?),
|
|
114
|
+
provider_session_id = COALESCE(provider_session_id, ?),
|
|
115
|
+
provider_lookup_status = CASE
|
|
116
|
+
WHEN provider_lookup_status = 'complete' THEN provider_lookup_status
|
|
117
|
+
ELSE COALESCE(?, provider_lookup_status)
|
|
118
|
+
END,
|
|
119
|
+
provider_status = COALESCE(provider_status, ?),
|
|
120
|
+
provider_error_code = CASE
|
|
121
|
+
WHEN provider_reason_source = 'provider_session_api' THEN provider_error_code
|
|
122
|
+
ELSE COALESCE(?, provider_error_code)
|
|
123
|
+
END,
|
|
124
|
+
provider_error_message = CASE
|
|
125
|
+
WHEN provider_reason_source = 'provider_session_api' THEN provider_error_message
|
|
126
|
+
ELSE COALESCE(?, provider_error_message)
|
|
127
|
+
END,
|
|
128
|
+
provider_reason_source = CASE
|
|
129
|
+
WHEN provider_reason_source = 'provider_session_api' THEN provider_reason_source
|
|
130
|
+
ELSE COALESCE(?, provider_reason_source)
|
|
131
|
+
END,
|
|
132
|
+
provider_duration_ms = COALESCE(provider_duration_ms, ?),
|
|
133
|
+
provider_bandwidth_bytes = COALESCE(provider_bandwidth_bytes, ?),
|
|
134
|
+
provider_captcha = COALESCE(provider_captcha, ?),
|
|
135
|
+
provider_checked_at = COALESCE(provider_checked_at, ?),
|
|
136
|
+
provider_detail_json = COALESCE(provider_detail_json, ?),
|
|
137
|
+
provider_disconnect_observed = COALESCE(provider_disconnect_observed, ?),
|
|
138
|
+
provider_disconnect_message = COALESCE(provider_disconnect_message, ?),
|
|
139
|
+
debug_json = ?,
|
|
140
|
+
completed_at = ?
|
|
141
|
+
WHERE job_id = ? AND attempt_number = ?
|
|
142
|
+
`,args:[e.outcome,e.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,e.questionCount,e.durationMs,e.error,e.willRetry?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded==null?null:e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError,e.browserCloseSucceeded==null?null:e.browserCloseSucceeded?1:0,e.browserCloseError,e.browserProvider??null,e.providerSessionId??null,e.providerLookupStatus??null,e.providerStatus??null,e.providerErrorCode??null,e.providerErrorMessage??null,e.providerReasonSource??null,e.providerDurationMs??null,e.providerBandwidthBytes??null,e.providerCaptcha==null?null:e.providerCaptcha?1:0,e.providerCheckedAt??null,e.providerDetail==null?null:vo(e.providerDetail),e.providerDisconnectObserved==null?null:e.providerDisconnectObserved?1:0,e.providerDisconnectMessage??null,e.debug==null?null:JSON.stringify(e.debug),e.completedAt,e.jobId,e.attemptNumber]})}function vo(e,t=4096){let r=JSON.stringify(e);return r.length<=t?r:JSON.stringify({truncated:!0})}async function Si(e){return(await m().execute({sql:`
|
|
143
|
+
UPDATE harvest_attempts SET
|
|
144
|
+
provider_lookup_status = ?,
|
|
145
|
+
provider_status = ?,
|
|
146
|
+
provider_error_code = ?,
|
|
147
|
+
provider_error_message = ?,
|
|
148
|
+
provider_reason_source = ?,
|
|
149
|
+
provider_duration_ms = ?,
|
|
150
|
+
provider_bandwidth_bytes = ?,
|
|
151
|
+
provider_captcha = ?,
|
|
152
|
+
provider_checked_at = ?,
|
|
153
|
+
provider_detail_json = ?
|
|
154
|
+
WHERE id = ?
|
|
155
|
+
AND job_id = ?
|
|
156
|
+
AND user_id = ?
|
|
157
|
+
AND browser_provider = '\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api'
|
|
158
|
+
AND provider_session_id = ?
|
|
159
|
+
AND COALESCE(provider_lookup_status, '') <> 'complete'
|
|
160
|
+
`,args:[e.lookupStatus,e.providerStatus??null,e.errorCode??null,e.errorMessage??null,e.reasonSource??null,e.durationMs??null,e.bandwidthBytes??null,e.captcha==null?null:e.captcha?1:0,e.checkedAt,e.detail==null?null:vo(e.detail),e.attemptId,e.jobId,e.userId,e.providerSessionId]})).rowsAffected===1}async function Ai(e=25){let t=Math.max(1,Math.min(100,Math.trunc(e)));return(await m().execute({sql:`
|
|
161
|
+
SELECT * FROM harvest_attempts
|
|
162
|
+
WHERE browser_provider = '\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api'
|
|
163
|
+
AND provider_session_id IS NOT NULL
|
|
164
|
+
AND provider_lookup_status IN ('pending', 'temporary_error')
|
|
165
|
+
ORDER BY COALESCE(provider_checked_at, created_at) ASC
|
|
166
|
+
LIMIT ?
|
|
167
|
+
`,args:[t]})).rows.map(o=>_o(o))}async function wo(e,t){return(t==null?await m().execute({sql:"SELECT * FROM harvest_attempts WHERE job_id = ? ORDER BY attempt_number ASC",args:[e]}):await m().execute({sql:`
|
|
168
|
+
SELECT ha.*
|
|
169
|
+
FROM harvest_attempts ha
|
|
170
|
+
INNER JOIN jobs j ON j.id = ha.job_id
|
|
171
|
+
WHERE ha.job_id = ? AND j.user_id = ?
|
|
172
|
+
ORDER BY ha.attempt_number ASC
|
|
173
|
+
`,args:[e,t]})).rows.map(o=>_o(o))}import{createHash as Di}from"crypto";var _n=1e9;function It(e){return e==="timeout"?"timed_out":e==="request_aborted"?"cancelled":e==="browser_session_interrupted"?"interrupted":e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"||e==="location_mismatch"||e==="error"?"failed":"succeeded"}function yn(e){return e==="\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061":"\u006b\u0065\u0072\u006e\u0065\u006c"}function Eo(e,t,r=0){let o=new Map,n=null;return async i=>{if(e)try{if(i.type==="started"){let a=r+i.attemptNumber;if(a>1&&!n){let c=(await We(e))?.attempts.at(-1);n=c?.id==null?null:String(c.id)}let l=await ie({runId:e,attemptNumber:a,method:i.method,provider:i.provider,parentAttemptId:a===1?null:n,edgeKind:a===1?null:r>0&&i.attemptNumber===1?"durable_resume":i.retryKind==="provider_fallback"?"provider_fallback":"same_method_retry",retryKind:a===1?null:r>0&&i.attemptNumber===1?"durable_resume":i.retryKind,retryReason:a===1?null:r>0&&i.attemptNumber===1?"durable_worker_resume":i.retryReason,retryOwner:a===1?null:r>0&&i.attemptNumber===1?"durable_reconciler":"harvest",normalizedFlags:{maxAttempts:i.maxAttempts,maxQuestions:i.maxQuestions,hasLocation:!!i.location,plannedProvider:i.provider,planReason:i.planReason??null,planTimeoutMs:i.planTimeoutMs??null},startedAt:i.startedAt}),u=await X({attemptId:l,provider:i.provider,providerProduct:i.provider==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:i.provider==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?Re("https://www.google.com/search"):null,accountScope:t,method:i.method,providerStatus:"started"});o.set(i.attemptNumber,{attemptId:l,receiptId:u.id,plannedProvider:i.provider,observedProvider:null,providerMismatch:!1,method:i.method,startedAtMs:Date.parse(i.startedAt),billingMode:null}),n=l;return}let s=o.get(i.attemptNumber);if(!s)throw new Error(`Missing operation attempt state for attempt ${i.attemptNumber}`);if(i.type==="provider_session_started"){let a=yn(i.provider);if(s.observedProvider=a,s.billingMode=i.billingMode??null,a!==s.plannedProvider){s.providerMismatch=!0,await Q({receiptId:s.receiptId,costState:"unavailable",providerStatus:"provider_mismatch",errorCode:"provider_mismatch",errorMessage:`Planned ${s.plannedProvider}; observed ${a}`,finalizedAt:i.observedAt});let l=a==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",u=await X({attemptId:s.attemptId,provider:a,providerProduct:a==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:a==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?Re("https://www.google.com/search"):null,accountScope:t,method:l,providerStatus:"observed_provider_mismatch"});s.receiptId=u.id,s.method=l}await Y(s.receiptId,i.providerSessionId);return}if(await ae({attemptId:s.attemptId,status:s.providerMismatch?"failed":It(i.outcome),errorCode:s.providerMismatch?"provider_mismatch":It(i.outcome)==="succeeded"?null:i.outcome,errorMessage:i.error,durationMs:i.durationMs,endedAt:i.completedAt}),(s.observedProvider??s.plannedProvider)==="\u006b\u0065\u0072\u006e\u0065\u006c"){let a=s.billingMode==="headful",l=s.billingMode==="headless"||s.billingMode==="headful"?or(i.durationMs,a):null;await Q({receiptId:s.receiptId,costState:"estimated",units:i.durationMs/1e3,unitType:"wall_open_second",amountUsdNanos:l==null?null:Math.round(l*_n),providerProduct:"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:s.billingMode,billableEvent:"provider_active_gb_second",measurementSource:"wall_open_time_proxy",rateSource:l!=null?`${rr};\u006b\u0065\u0072\u006e\u0065\u006c_usd_per_second:${tr}`:null,providerStatus:s.providerMismatch?"provider_mismatch":i.outcome,providerDurationMs:i.durationMs,errorCode:s.providerMismatch?"provider_mismatch":It(i.outcome)==="succeeded"?null:i.outcome,errorMessage:i.error,finalizedAt:i.completedAt})}}catch(s){console.warn(JSON.stringify({event:"operation_attempt_telemetry_failed",operation_run_id:e,attempt_number:i.attemptNumber,message:s instanceof Error?s.message:String(s)}))}}}function So(e,t,r=null,o="production-default",n=0){let i=Eo(r,o,n);return async s=>{if(await i(s),s.type==="started"){await yo({jobId:e,userId:t,attemptNumber:s.attemptNumber,maxAttempts:s.maxAttempts,query:s.query,location:s.location,maxQuestions:s.maxQuestions,startedAt:s.startedAt});return}if(s.type==="provider_session_started"){await bo({jobId:e,attemptNumber:s.attemptNumber,browserProvider:s.provider,providerSessionId:s.providerSessionId,observedAt:s.observedAt});return}await ho({jobId:e,attemptNumber:s.attemptNumber,outcome:s.outcome,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:s.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:s.questionCount,durationMs:s.durationMs,error:s.error,willRetry:s.willRetry,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:s.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:s.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:s.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError,browserCloseSucceeded:s.cleanup.browserCloseSucceeded,browserCloseError:s.cleanup.browserCloseError,browserProvider:s.cleanup.provider??null,providerSessionId:s.cleanup.providerSessionId??null,providerLookupStatus:s.cleanup.providerSessionId?"pending":"not_applicable",providerErrorCode:s.cleanup.providerDisconnectObserved?"browser_session_interrupted":null,providerErrorMessage:s.cleanup.providerDisconnectMessage??null,providerReasonSource:s.cleanup.providerDisconnectObserved?"client_runtime":null,providerDisconnectObserved:s.cleanup.providerDisconnectObserved??!1,providerDisconnectMessage:s.cleanup.providerDisconnectMessage??null,debug:s.debug,completedAt:s.completedAt})}}function bn(e){return e instanceof Error?e.message:String(e)}function hn(e,t){return e instanceof DOMException&&(e.name==="TimeoutError"||e.name==="AbortError")?!0:/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(t)}function vn(e){return/captcha|recaptcha|unusual traffic|google\.com\/sorry|blocked/i.test(e)}function wn(e){return/ERR_TUNNEL_CONNECTION_FAILED|ERR_PROXY_CONNECTION_FAILED|ERR_SOCKS_CONNECTION_FAILED|tunnel connection failed|proxy connection failed|transport error: proxy/i.test(e)}function En(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function Sn(e){return/(?:spending|billing|organization|account).{0,80}(?:cap|limit|blocked|disabled)|(?:quota|capacity).{0,40}(?:reached|exceeded)|https?:\/\/\S*dashboard/i.test(e)}function An(e){return/browser (?:has been )?closed|context (?:has been )?closed|target page, context or browser has been closed|session closed|page has been closed/i.test(e)}function Rt(e){let t=bn(e);return e instanceof Vt?{error_code:"request_aborted",error_type:"request_aborted",message:U("request_aborted"),retryable:!0,httpStatus:408,terminalStatus:"cancelled"}:e instanceof zt||vn(t)?{error_code:"captcha_exhausted",error_type:"captcha",message:U("captcha_exhausted"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:_r(e,t)?{error_code:"vendor_unavailable",error_type:"service_unavailable",message:U("vendor_unavailable"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:e instanceof Kt?{error_code:"location_mismatch",error_type:"location_mismatch",message:U("location_mismatch"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:wn(t)?{error_code:"proxy_tunnel_failed",error_type:"connection",message:U("proxy_tunnel_failed"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:En(t)?{error_code:"proxy_unavailable",error_type:"connection",message:U("proxy_unavailable"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:An(t)?{error_code:"browser_session_interrupted",error_type:"extraction",message:U("browser_session_interrupted"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:hn(e,t)?{error_code:"harvest_timeout",error_type:"timeout",message:U("harvest_timeout"),retryable:!0,httpStatus:504,terminalStatus:"failed"}:Sn(t)?{error_code:"service_unavailable",error_type:"service_unavailable",message:ct,retryable:!0,httpStatus:503,terminalStatus:"failed"}:{error_code:"extraction_failed",error_type:"extraction",message:ct,retryable:!1,httpStatus:500,terminalStatus:"failed"}}function Fi(e){return JSON.stringify({error_code:e.error_code,error_type:e.error_type,message:e.message,retryable:e.retryable})}function Ao(e,t={}){let r=B({errorCode:e.error_code,errorType:e.error_type,retryable:e.retryable,retryAfterSeconds:t.retryAfterSeconds,chargeStatus:t.chargeStatus,details:t.details});return{error:r.message,...r}}function Io(e,t={}){let{error:r,...o}=Ao(e,t);return o}function Ui(e,t={}){let r=Rt(e);return{body:Ao(r,t),status:r.httpStatus}}import{randomUUID as Ro}from"crypto";var In=3e4;async function No(e,t,r){if(!e.receiptJson)return;let o=Pe(e.id);if((await T(o,e.userId,"paa_callback",e.jobId))?.phase==="published")return;if(r){if(await $e(r,{status:"succeeded",json:e.receiptJson}))return;await ne(r)}let i=await Ue(o,e.userId);i&&await Je(i,{status:"unknown",json:e.receiptJson},Oe(e,t,"accepted"))}function Rn(e){return!!(e.payloadJson&&e.payloadSha256===O(e.payloadJson)&&e.sendNonce&&e.sendToken&&e.httpStatus&&e.httpStatus>=200&&e.httpStatus<300&&e.receiptJson===h({deliveryId:e.id,idempotencyKey:e.id,payloadSha256:e.payloadSha256,httpStatus:e.httpStatus}))}async function ko(e){let t=e.record,r,o=!1;try{if(t=await j(t.id,t.userId)??t,t.state==="accepted")return Rn(t)?(await No(t,e.domain).catch(()=>{}),L(t)):{state:"unavailable",deliveryId:t.id};if(["sending","unknown","expired","unavailable"].includes(t.state))return L(t);if(t.expiresAtMs<=t.nowMs)return await ot(t,"expired"),L(await j(t.id,t.userId));if(t.payloadJson||(t=await Xr(t,await e.compose(),e.domain)),!t.payloadJson||t.payloadSha256!==O(t.payloadJson))throw Error("Delivery payload conflicts");let n=Oe(t,e.domain,"frozen"),i=Pe(t.id),s={version:1,purpose:"outbound-callback",request:$(t),payloadSha256:t.payloadSha256},a=await Ae({id:i,userId:t.userId,ownerScope:`user:${t.userId}`,tool:"paa_callback",jobId:t.jobId,idempotencyKey:t.id,requestFingerprint:O(h(s)),normalizedFlags:s},Ro(),In,n);if(a.kind!=="claimed"){let u=await T(i,t.userId,"paa_callback",t.jobId);if(u&&["effects_started","unknown","published"].includes(u.phase)){await vt(t),u.phase==="published"&&await ot(t,"unavailable",n);let d=await j(t.id,t.userId);return d?L(d):{state:"unknown",deliveryId:t.id}}return{state:"pending",deliveryId:t.id,expiresAtMs:t.expiresAtMs}}if(r=a.claim,!await e.validate(t.url))return L(t);if(!await Be(r,n))throw Error("Delivery effects start is unconfirmed");if(o=!0,t=await Yr(t,r,Ro(),e.domain),!await Qr(t,r,e.domain))throw Error("Delivery authority was lost before send");let l=await fetch(t.url,{method:"POST",headers:{"content-type":"application/json","idempotency-key":t.id},body:t.payloadJson,redirect:"error",signal:AbortSignal.timeout(1e4)});if(!l.ok||l.status<200||l.status>=300)throw Error("Recipient acceptance is unconfirmed");return t=await Zr(t,l.status),await No(t,e.domain,r).catch(()=>{}),L(t)}catch(n){if(n instanceof ee&&await ot(t,"unavailable",e.domain).catch(()=>{}),r){let s=await T(r.runId,r.userId,"paa_callback",t.jobId).catch(()=>null);(o||s?.phase==="effects_started"||s?.phase==="unknown")&&await ne(r).catch(()=>{})}t.sendNonce?await eo(t).catch(()=>{}):await vt(t).catch(()=>{});let i=await j(t.id,t.userId).catch(()=>null);return i?L(i):{state:"unknown",deliveryId:t.id}}finally{r&&!o&&await Se(r).catch(()=>{})}}async function Nt(e,t){let r=await no(e,t);return r.kind==="none"?{state:"none"}:r.kind==="unavailable"?{state:"unavailable"}:r.kind==="pending"?(await L(r.record),{state:"pending",deliveryId:r.record.id,expiresAtMs:r.record.expiresAtMs}):ko({record:r.record,domain:r.domain,validate:async o=>{let n=await gr(o,{field:"callback_url",requireHttps:!0});return!n.error&&n.parsed?.href===o&&!n.parsed.username&&!n.parsed.password},compose:async()=>{let o=(await it(r.job,!1)).body;return{...o,...o.result?{result:sr(o.result)}:{},...r.record.profile==="paa-queue"?{attempts:nr(await wo(r.job.id,r.job.user_id))}:{}}}})}async function ra(e=25){let t=0,r=0,o=await to(e);for(let n of o)try{(await Nt(n.jobId,n.userId)).state==="accepted"?t++:r++}catch{r++}return{checked:o.length,accepted:t,pending:r}}var xo=2,kt=0;async function Do(){let e=await mo(),t=e?void 0:await br(),r=e??(t?await S(t.id,Number(t.user_id)):void 0);if(!r)return{claimed:!1};let o=Date.now();kt++;try{let n=await go(r,{\u006b\u0065\u0072\u006e\u0065\u006cApiKey:Gt(),apifyToken:process.env.APIFY_API_TOKEN?.trim(),onAttemptEvent:So(r.id,r.user_id,`harvest:${r.id}`),withCostContext:Qt,classifyFailure:i=>{let s=Rt(i);return{publicError:Io(s),httpStatus:s.httpStatus,terminalStatus:s.terminalStatus}}});return n.completed&&r.callback_url&&await Nt(r.id,r.user_id).catch(()=>{}),{claimed:!0,jobId:r.id,completed:n.completed,durationMs:Date.now()-o}}finally{kt--}}async function pa(e){let t=[];for(let r=0;r<e.maxJobs&&!(Date.now()>=e.deadlineMs);r++){let o=await Do();if(t.push(o),!o.claimed)break}return t}function ma(){setInterval(async()=>{kt>=xo||Do()},2e3),console.log(`[worker] started \u2014 polling every 2s, max ${xo} concurrent`)}export{Tn as a,Cn as b,jn as c,qn as d,Mn as e,Ln as f,Hn as g,Fn as h,zr as i,tt as j,Re as k,Ke as l,Uo as m,Qn as n,Si as o,Ai as p,wo as q,Rt as r,Fi as s,Io as t,Ui as u,ro as v,oo as w,Gr as x,ht as y,W as z,mn as A,nt as B,it as C,st as D,St as E,co as F,Nt as G,ra as H,Gn as I,hr as J,hi as K,gn as L,So as M,Do as N,pa as O,ma as P};
|