1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#![forbid(unsafe_code)]
mod bench;
mod cli;
use std::io::Read;
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::Arc;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use pagesift_content::{Options, process};
use tracing_subscriber::EnvFilter;
use url::Url;
#[derive(Parser)]
#[command(
name = "pagesift",
version,
about = "Turn web pages into clean Markdown for LLMs"
)]
struct Cli {
/// Config file (default: $PAGESIFT_CONFIG); see pagesift.example.toml
#[arg(long, global = true)]
config: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Convert a local HTML file (or stdin) to Markdown
Convert {
/// HTML file to read; reads stdin when omitted or "-"
file: Option<PathBuf>,
/// URL the HTML was fetched from, used to make links absolute
#[arg(long)]
base_url: Option<Url>,
/// Keep the whole page instead of only the main content
#[arg(long)]
full: bool,
},
/// Start the Firecrawl-compatible HTTP API
Serve {
/// Port (default: server.port in the config, else 3002)
#[arg(long)]
port: Option<u16>,
/// Address to listen on (default: server.host in the config, else 127.0.0.1)
#[arg(long)]
host: Option<String>,
/// Require `Authorization: Bearer <key>` (repeatable); also read from PAGESIFT_API_KEYS (comma separated)
#[arg(long = "api-key")]
api_keys: Vec<String>,
/// Public base URL used in job links (default: from the Host header)
#[arg(long)]
public_url: Option<String>,
/// Where the cache lives (default: $PAGESIFT_DATA_DIR, else ~/.local/share/pagesift)
#[arg(long)]
data_dir: Option<PathBuf>,
/// Disable the result cache
#[arg(long)]
no_cache: bool,
},
/// Scrape a single URL
Scrape {
url: String,
/// Output: md prints the Markdown, json prints the Firecrawl response
#[arg(long, default_value = "md")]
format: String,
/// http, browser or auto
#[arg(long, default_value = "auto")]
engine: String,
/// Keep the whole page instead of only the main content
#[arg(long)]
full: bool,
/// Ignore robots.txt for this page (single-page scrapes only)
#[arg(long)]
ignore_robots: bool,
},
/// Crawl a site; writes one Markdown file per page with --out, else JSON lines to stdout
Crawl {
url: String,
#[arg(long, default_value_t = 100)]
limit: usize,
/// Directory for the Markdown files and an index.jsonl
#[arg(long)]
out: Option<PathBuf>,
/// Maximum discovery depth from the start page
#[arg(long)]
depth: Option<usize>,
/// Only follow paths matching this regex (repeatable)
#[arg(long)]
include: Vec<String>,
/// Skip paths matching this regex (repeatable)
#[arg(long)]
exclude: Vec<String>,
/// Crawl the whole domain, not just pages under the start path
#[arg(long)]
entire_domain: bool,
/// Follow links to subdomains
#[arg(long)]
subdomains: bool,
/// include, skip or only
#[arg(long, default_value = "include")]
sitemap: String,
/// http, browser or auto
#[arg(long, default_value = "auto")]
engine: String,
/// Crawl the pages most relevant to this query first
#[arg(long)]
query: Option<String>,
/// With --query: stop early once the collected pages cover the query
#[arg(long, requires = "query")]
adaptive: bool,
},
/// List the URLs of a site
Map {
url: String,
#[arg(long, default_value_t = 5000)]
limit: usize,
/// Rank URLs matching these words first
#[arg(long)]
search: Option<String>,
/// include, skip or only
#[arg(long, default_value = "include")]
sitemap: String,
},
/// Run the MCP server (stdio by default)
Mcp {
/// stdio or http
#[arg(default_value = "stdio")]
transport: String,
/// Port for the http transport (listens on 127.0.0.1)
#[arg(long, default_value_t = 3003)]
port: u16,
},
/// Check the environment
Doctor,
/// Evaluate content extraction on local dataset snapshots
Bench {
/// Directory holding 1-0-0.csv and snapshots/ (see bench/fetch_snapshots.py)
dir: PathBuf,
/// Write per-page results as JSON lines
#[arg(long)]
out: Option<PathBuf>,
/// Timing runs per page; the fastest is reported
#[arg(long, default_value_t = 3)]
runs: usize,
/// Only evaluate the first N dataset rows
#[arg(long)]
limit: Option<usize>,
/// Simulate `engine: auto` using browser-rendered snapshots (see bench-render)
#[arg(long)]
auto: bool,
/// Write the HTML each page was evaluated on, plus a manifest, for other tools
#[arg(long)]
emit_inputs: Option<PathBuf>,
/// Score another tool's output (JSON lines with id, markdown, ms) instead of pagesift
#[arg(long)]
external: Option<PathBuf>,
/// Threads for the throughput run; 0 uses every core
#[arg(long, default_value_t = 0)]
jobs: usize,
},
/// Render thin snapshots with a browser for `bench --auto`
#[command(hide = true)]
BenchRender {
dir: PathBuf,
#[arg(long)]
limit: Option<usize>,
#[arg(long, default_value_t = 8)]
concurrency: usize,
},
}
fn main() -> ExitCode {
pagesift_sys::tune_allocator();
let cli = Cli::parse();
let cfg = match pagesift::config::Config::load(cli.config.as_deref()) {
Ok(c) => c,
Err(e) => {
eprintln!("error: {e:#}");
return ExitCode::FAILURE;
}
};
// 结构化日志:PAGESIFT_LOG 定级别,PAGESIFT_LOG_FORMAT 或 [log] format 选 text / json
let json = match cfg.log_json() {
Ok(j) => j,
Err(e) => {
eprintln!("error: {e:#}");
return ExitCode::FAILURE;
}
};
let logs = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_env("PAGESIFT_LOG"))
.with_writer(std::io::stderr);
if json {
logs.json().flatten_event(true).init();
} else {
logs.init();
}
match run(cli.command, cfg) {
Ok(code) => code,
Err(e) => {
eprintln!("error: {e:#}");
ExitCode::FAILURE
}
}
}
fn run(command: Command, mut cfg: pagesift::config::Config) -> Result<ExitCode> {
match command {
Command::Convert {
file,
base_url,
full,
} => {
let html = read_input(file)?;
let doc = process(
&html,
&Options {
base_url,
only_main_content: !full,
..Default::default()
},
);
print!("{}", doc.markdown);
Ok(ExitCode::SUCCESS)
}
Command::Bench {
dir,
out,
runs,
limit,
auto,
emit_inputs,
external,
jobs,
} => {
bench::run(bench::Args {
dir,
out,
runs,
limit,
auto,
emit_inputs,
external,
jobs,
})?;
Ok(ExitCode::SUCCESS)
}
Command::BenchRender {
dir,
limit,
concurrency,
} => {
bench::render_snapshots(&dir, limit, concurrency)?;
Ok(ExitCode::SUCCESS)
}
Command::Scrape {
url,
format,
engine,
full,
ignore_robots,
} => runtime()?.block_on(async move {
let scraper = pagesift::Scraper::new(cfg.scraper(false)?)?;
let mut opts = pagesift::api::types::ScrapeOptions {
only_main_content: !full,
ignore_robots_txt: ignore_robots,
engine: Some(engine),
..Default::default()
};
if format == "json" {
opts.formats
.push(pagesift::api::types::Format::new("links"));
}
let result = scraper.scrape(&url, &opts).await;
scraper.shutdown().await;
match result {
Ok(doc) if format == "json" => {
let body = serde_json::json!({ "success": true, "data": doc });
println!("{}", serde_json::to_string_pretty(&body)?);
Ok(ExitCode::SUCCESS)
}
Ok(doc) => {
print!("{}", doc.markdown.unwrap_or_default());
Ok(ExitCode::SUCCESS)
}
Err(e) => {
eprintln!("{}: {}", e.code.as_str(), e.message);
if let Some(d) = &e.details {
eprintln!("{d}");
}
Ok(ExitCode::from(1))
}
}
}),
Command::Serve {
port,
host,
api_keys,
public_url,
data_dir,
no_cache,
} => runtime()?.block_on(async move {
if data_dir.is_some() {
cfg.cache.dir = data_dir;
}
// 缓存和任务共用数据目录里的一个存储
let cache = !no_cache && cfg.cache.enabled.unwrap_or(true);
let persist = cfg.server.persist_jobs.unwrap_or(true);
let store = if cache || persist {
Some(cfg.open_store()?)
} else {
None
};
let mut config = cfg.scraper(false)?;
config.store = store.clone().filter(|_| cache);
if store.is_some() {
eprintln!(
"data: {} (cache {}, jobs {})",
cfg.data_dir().join("store").display(),
if cache { "on" } else { "off" },
if persist { "persisted" } else { "in memory" }
);
}
let mut api = cfg.api()?;
api.api_keys.extend(api_keys);
if public_url.is_some() {
api.public_url = public_url;
}
api.store = store.filter(|_| persist);
let host = host
.or(cfg.server.host.clone())
.unwrap_or_else(|| "127.0.0.1".into());
let port = port.or(cfg.server.port).unwrap_or(3002);
let scraper = Arc::new(pagesift::Scraper::new(config)?);
// 后台确认每个浏览器内核能不能启动,结果写进日志,不耽误服务开始监听
let probe = scraper.clone();
tokio::spawn(async move { probe.probe_browsers().await });
let app = pagesift::api::router(scraper.clone(), api);
let listener = tokio::net::TcpListener::bind((host.as_str(), port))
.await
.with_context(|| format!("binding {host}:{port}"))?;
eprintln!("pagesift listening on http://{}", listener.local_addr()?);
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
scraper.shutdown().await;
Ok(ExitCode::SUCCESS)
}),
Command::Crawl {
url,
limit,
out,
depth,
include,
exclude,
entire_domain,
subdomains,
sitemap,
engine,
query,
adaptive,
} => runtime()?.block_on(async move {
let req: pagesift::api::types::CrawlRequest =
serde_json::from_value(serde_json::json!({
"url": url,
"limit": limit,
"maxDiscoveryDepth": depth,
"includePaths": include,
"excludePaths": exclude,
"crawlEntireDomain": entire_domain,
"allowSubdomains": subdomains,
"sitemap": sitemap,
"scrapeOptions": { "engine": engine },
"query": query,
"adaptive": adaptive,
}))?;
if let Some(dir) = &out {
std::fs::create_dir_all(dir)
.with_context(|| format!("creating {}", dir.display()))?;
}
let scraper = pagesift::Scraper::new(cfg.scraper(false)?)?;
let sink = Arc::new(cli::CrawlOutput::new(out));
let result = scraper.crawl(&req, sink.clone()).await;
scraper.shutdown().await;
let (pages, errors) = sink.counts();
eprintln!("crawled {pages} pages, {errors} errors");
match result {
Ok(()) => Ok(ExitCode::SUCCESS),
Err(e) => {
eprintln!("{}: {}", e.code.as_str(), e.message);
Ok(ExitCode::from(1))
}
}
}),
Command::Map {
url,
limit,
search,
sitemap,
} => runtime()?.block_on(async move {
let req: pagesift::api::types::MapRequest =
serde_json::from_value(serde_json::json!({
"url": url, "limit": limit, "search": search, "sitemap": sitemap,
}))?;
let scraper = pagesift::Scraper::new(cfg.scraper(false)?)?;
match scraper.map(&req).await {
Ok(links) => {
for l in links {
println!("{}", l.url);
}
Ok(ExitCode::SUCCESS)
}
Err(e) => {
eprintln!("{}: {}", e.code.as_str(), e.message);
Ok(ExitCode::from(1))
}
}
}),
Command::Doctor => runtime()?.block_on(cli::doctor(&cfg)),
Command::Mcp { transport, port } => runtime()?.block_on(async move {
let scraper = Arc::new(pagesift::Scraper::new(cfg.scraper(false)?)?);
let server = pagesift_mcp::McpServer::builder("pagesift", env!("CARGO_PKG_VERSION"))
.tools(pagesift::mcp::tools(scraper.clone()))
.build()
.map_err(|e| anyhow::anyhow!("building the MCP server: {e:?}"))?;
match transport.as_str() {
"stdio" => pagesift_mcp::serve_stdio(server).await?,
"http" => {
let config = pagesift_mcp::HttpConfig {
bind: std::net::SocketAddr::from(([127, 0, 0, 1], port)),
..Default::default()
};
eprintln!(
"pagesift MCP listening on http://127.0.0.1:{port}{}",
config.path
);
pagesift_mcp::serve_http(server, config).await?;
}
other => anyhow::bail!("unknown transport {other:?}: use stdio or http"),
}
scraper.shutdown().await;
Ok(ExitCode::SUCCESS)
}),
}
}
/// Ctrl-C,或者 SIGTERM(`docker stop`、systemd)。进行中的任务已经落盘,下次启动接着跑。
async fn shutdown_signal() {
#[cfg(unix)]
{
use tokio::signal::unix::{SignalKind, signal};
match signal(SignalKind::terminate()) {
Ok(mut term) => {
tokio::select! {
_ = tokio::signal::ctrl_c() => {}
_ = term.recv() => {}
}
}
Err(_) => {
let _ = tokio::signal::ctrl_c().await;
}
}
}
#[cfg(not(unix))]
{
let _ = tokio::signal::ctrl_c().await;
}
tracing::info!("shutting down");
}
fn runtime() -> Result<tokio::runtime::Runtime> {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.context("starting the async runtime")
}
fn read_input(file: Option<PathBuf>) -> Result<String> {
let bytes = match file {
Some(path) if path.as_os_str() != "-" => {
std::fs::read(&path).with_context(|| format!("reading {}", path.display()))?
}
_ => {
let mut buf = Vec::new();
std::io::stdin()
.read_to_end(&mut buf)
.context("reading stdin")?;
buf
}
};
// 字符集识别在 M3 网络层做,这里先按 UTF-8 宽松解码
Ok(String::from_utf8_lossy(&bytes).into_owned())
}