crawlforge-mcp-server 5.6.11 → 5.9.0
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/CLAUDE.md +5 -4
- package/README.md +8 -5
- package/package.json +3 -3
- package/server.js +59 -18
- package/src/core/AuthManager.js +16 -1
- package/src/core/ResultStore.js +230 -0
- package/src/schemas/toolOutputSchemas.js +89 -13
- package/src/server/fallbackHints.js +35 -9
- package/src/server/inlineThreshold.js +149 -0
- package/src/server/toolFilter.js +11 -4
- package/src/server/withAuth.js +25 -0
- package/src/skills/agent-skills/crawlforge-batch-automation/SKILL.md +2 -1
- package/src/skills/agent-skills/crawlforge-getting-started/SKILL.md +6 -4
- package/src/skills/agent-skills/crawlforge-getting-started/references/credits.md +8 -1
- package/src/skills/agent-skills/crawlforge-stealth-browsing/SKILL.md +5 -0
- package/src/skills/agent-skills/crawlforge-web-scraping/SKILL.md +73 -2
- package/src/skills/agent-skills/crawlforge-web-scraping/references/tool-reference.md +27 -1
- package/src/tools/advanced/batchScrape/index.js +23 -43
- package/src/tools/result/readResult.js +147 -0
- package/src/tools/scrape/_highlights.js +79 -0
- package/src/tools/scrape/escalation.js +62 -0
- package/src/tools/scrape/formats.js +47 -1
- package/src/tools/scrape/unifiedScrape.js +345 -55
|
@@ -8,9 +8,10 @@ CrawlForge credits.
|
|
|
8
8
|
| Param | Type | Default | Notes |
|
|
9
9
|
|-------|------|---------|-------|
|
|
10
10
|
| `url` | string (URL) | — | Required. |
|
|
11
|
-
| `formats` | array | `["markdown"]` | Any of `markdown`, `html`, `rawHtml`, `text`, `links`, `metadata`, `screenshot`,
|
|
11
|
+
| `formats` | array | `["markdown"]` | Any of `markdown`, `html`, `rawHtml`, `text`, `links`, `metadata`, `branding`, `screenshot`, `{type:"json", schema?, prompt?}`, `{type:"highlights", query, max_highlights?, mode?}` (the matching sentences, table rows and code blocks, verbatim, with offsets into the markdown; +1 credit once per call) or `{type:"question", question, mode?}` (an answer assembled from the evidence units; same +1). `mode:"model"` adds 3 credits and needs an LLM route. |
|
|
12
12
|
| `onlyMainContent` | boolean | `true` | Strip boilerplate via Readability. |
|
|
13
13
|
| `timeoutMs` | number | `15000` | 1000–60000. |
|
|
14
|
+
| `max_inline_chars` | number | `40000` | 1,000–10,000,000. A larger result returns `preview` + `result_handle` for `read_result` (below). |
|
|
14
15
|
|
|
15
16
|
Partial success: a format that fails produces a `warnings[]` entry rather than
|
|
16
17
|
failing the whole call. `{type:"json"}` may incur external LLM cost (billed by
|
|
@@ -23,6 +24,7 @@ your provider).
|
|
|
23
24
|
| `url` | string (URL) | — | Required. |
|
|
24
25
|
| `headers` | object | — | Custom HTTP headers (e.g. auth tokens). |
|
|
25
26
|
| `timeout` | number | `10000` | 1000–30000 ms. |
|
|
27
|
+
| `max_inline_chars` | number | `40000` | 1,000–10,000,000. A larger result returns `preview` + `result_handle` for `read_result` (below). |
|
|
26
28
|
|
|
27
29
|
## extract_text (cost: 1)
|
|
28
30
|
|
|
@@ -53,6 +55,7 @@ your provider).
|
|
|
53
55
|
|-------|------|-------|
|
|
54
56
|
| `url` | string (URL) | Required. Readability-cleaned article body (markdown). |
|
|
55
57
|
| `options` | object | Additional extraction options. |
|
|
58
|
+
| `max_inline_chars` | number | Default 40000 (1,000–10,000,000). A larger result returns `preview` + `result_handle` for `read_result` (below). |
|
|
56
59
|
|
|
57
60
|
## map_site (cost: 2)
|
|
58
61
|
|
|
@@ -82,10 +85,33 @@ your provider).
|
|
|
82
85
|
| `concurrency` | number | — | 1–20 concurrent requests. |
|
|
83
86
|
| `enable_link_analysis` | boolean | — | Compute PageRank over crawled pages. |
|
|
84
87
|
| `session` | object | — | Shared cookie jar for login-then-crawl. |
|
|
88
|
+
| `max_inline_chars` | number | `40000` | 1,000–10,000,000. A larger result returns `preview` + `result_handle` for `read_result` (below). |
|
|
85
89
|
|
|
86
90
|
*Server default honors `RESPECT_ROBOTS_TXT`. Crawls projected over ~500 pages
|
|
87
91
|
trigger an elicitation confirmation.
|
|
88
92
|
|
|
93
|
+
## read_result (cost: 1)
|
|
94
|
+
|
|
95
|
+
Reads a result a tool returned with `truncated: true` and a `result_handle`
|
|
96
|
+
(kept 1 hour, on the local machine; an unknown or expired handle is an error).
|
|
97
|
+
|
|
98
|
+
| Param | Type | Default | Notes |
|
|
99
|
+
|-------|------|---------|-------|
|
|
100
|
+
| `handle` | string | — | Required. The `result_handle` (`res_…`). |
|
|
101
|
+
| `operation` | enum | — | Required. `slice`, `search`, `lines` or `json_path`. |
|
|
102
|
+
| `offset` | number | — | `slice`: character offset; `lines`: first line (default 0). |
|
|
103
|
+
| `length` | number | — | `slice`: characters (default 10,000); `lines`: lines (default 200, at most 5,000). |
|
|
104
|
+
| `query` | string | — | `search`: case-insensitive literal substring. |
|
|
105
|
+
| `max_matches` | number | `20` | `search`: 1–100. |
|
|
106
|
+
| `path` | string | — | `json_path`: path into the stored result object (or the parsed body when the stored text is JSON, e.g. a `fetch_url` body). |
|
|
107
|
+
| `max_inline_chars` | number | `40000` | 1,000–10,000,000. |
|
|
108
|
+
|
|
109
|
+
Every response carries `handle`, `tool`, `operation`, `view` (`text` or `json`),
|
|
110
|
+
`view_path`, `total_chars` and `expires_at`. `slice` returns `text` + `has_more`;
|
|
111
|
+
`search` returns `matches[{offset, length, context_offset, context}]` (200
|
|
112
|
+
characters of context each side) + `total_matches`; `lines` returns `lines[]`
|
|
113
|
+
+ `total_lines`, `char_offset`, `has_more`; `json_path` returns `value`.
|
|
114
|
+
|
|
89
115
|
## CLI quick map
|
|
90
116
|
|
|
91
117
|
| Tool | CLI |
|
|
@@ -18,6 +18,7 @@ import { EventEmitter } from 'events';
|
|
|
18
18
|
import { ElicitationHelper } from '../../../core/ElicitationHelper.js'; // D1.4
|
|
19
19
|
import JobManager from '../../../core/JobManager.js';
|
|
20
20
|
import WebhookDispatcher from '../../../core/WebhookDispatcher.js';
|
|
21
|
+
import { getResultStore } from '../../../core/ResultStore.js';
|
|
21
22
|
import { BatchScrapeSchema } from './schema.js';
|
|
22
23
|
import { scrapeUrlsBatch, processResults, paginateResults } from './queue.js';
|
|
23
24
|
import { sendWebhookNotification } from './reporter.js';
|
|
@@ -35,7 +36,7 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
35
36
|
maxBatchSize = 50,
|
|
36
37
|
enableResultCaching = true,
|
|
37
38
|
enableLogging = true,
|
|
38
|
-
|
|
39
|
+
resultStore = null
|
|
39
40
|
} = options;
|
|
40
41
|
|
|
41
42
|
this.jobManager = jobManager || new JobManager({
|
|
@@ -54,21 +55,12 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
54
55
|
this.enableWebhookNotifications = enableWebhookNotifications;
|
|
55
56
|
|
|
56
57
|
this.activeBatches = new Map();
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this.
|
|
58
|
+
// Phase 2: completed batches live in the shared ResultStore (keyed by
|
|
59
|
+
// batchId) so batch and single results share one eviction budget and TTL.
|
|
60
|
+
this.resultStore = resultStore || getResultStore();
|
|
60
61
|
// D1.4: Elicitation helper (set mcpServer after instantiation if desired)
|
|
61
62
|
this._elicitation = new ElicitationHelper({});
|
|
62
63
|
|
|
63
|
-
// Bound batchResults' lifetime: sweep expired entries periodically (in
|
|
64
|
-
// addition to the on-read eviction in getBatchResults) so cached results —
|
|
65
|
-
// including full HTML bodies when formats includes 'html' — don't
|
|
66
|
-
// accumulate in memory for the life of the process.
|
|
67
|
-
// .unref() so this timer never blocks process exit on its own — matches
|
|
68
|
-
// SnapshotManager's cleanupTimer.
|
|
69
|
-
this._resultsSweepTimer = setInterval(() => this._sweepBatchResults(), 10 * 60 * 1000);
|
|
70
|
-
if (typeof this._resultsSweepTimer.unref === 'function') this._resultsSweepTimer.unref();
|
|
71
|
-
|
|
72
64
|
this.stats = {
|
|
73
65
|
totalBatches: 0,
|
|
74
66
|
completedBatches: 0,
|
|
@@ -232,20 +224,18 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
232
224
|
}
|
|
233
225
|
|
|
234
226
|
async getBatchResults(batchId, page = 1, pageSize = 25) {
|
|
235
|
-
|
|
227
|
+
// An expired or unknown id reads as null (the store evicts on read) and
|
|
228
|
+
// falls through to the activeBatches/jobManager lookups below.
|
|
229
|
+
const cached = this.resultStore.get(batchId);
|
|
236
230
|
if (cached) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
// Expired — evict now instead of waiting for the periodic sweep, and
|
|
247
|
-
// fall through to the activeBatches/jobManager lookups below.
|
|
248
|
-
this.batchResults.delete(batchId);
|
|
231
|
+
const { results, mode } = cached.payload;
|
|
232
|
+
const offset = (page - 1) * pageSize;
|
|
233
|
+
return {
|
|
234
|
+
batchId, success: true, status: 'completed', ...(mode ? { mode } : {}),
|
|
235
|
+
results: paginateResults(results, offset, pageSize),
|
|
236
|
+
pagination: { page, pageSize, totalResults: results.length, totalPages: Math.ceil(results.length / pageSize) },
|
|
237
|
+
cached: true, timestamp: cached.createdAt
|
|
238
|
+
};
|
|
249
239
|
}
|
|
250
240
|
|
|
251
241
|
const active = this.activeBatches.get(batchId);
|
|
@@ -321,7 +311,7 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
321
311
|
return {
|
|
322
312
|
...this.stats,
|
|
323
313
|
activeBatches: this.activeBatches.size,
|
|
324
|
-
cachedResults: this.
|
|
314
|
+
cachedResults: this._cachedBatchIds().length,
|
|
325
315
|
jobManagerStats: this.jobManager ? this.jobManager.getStats() : null,
|
|
326
316
|
webhookStats: this.webhookDispatcher ? this.webhookDispatcher.getStats() : null
|
|
327
317
|
};
|
|
@@ -332,8 +322,7 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
332
322
|
try { await this.cancelBatch(batchId); } catch (e) { this._log('warn', `Failed to cancel batch ${batchId}: ${e.message}`); }
|
|
333
323
|
}
|
|
334
324
|
this.activeBatches.clear();
|
|
335
|
-
this.
|
|
336
|
-
if (this._resultsSweepTimer) clearInterval(this._resultsSweepTimer);
|
|
325
|
+
for (const batchId of this._cachedBatchIds()) this.resultStore.delete(batchId);
|
|
337
326
|
this.jobManager?.destroy();
|
|
338
327
|
this.webhookDispatcher?.destroy();
|
|
339
328
|
this.removeAllListeners();
|
|
@@ -365,23 +354,14 @@ export class BatchScrapeTool extends EventEmitter {
|
|
|
365
354
|
return this.webhookDispatcher.registerWebhook(webhookConfig.url, config);
|
|
366
355
|
}
|
|
367
356
|
|
|
368
|
-
/**
|
|
369
|
-
* Cache a batch's results, capped to maxCachedBatches. Map preserves
|
|
370
|
-
* insertion order, so the oldest entry is evicted first (LRU by write time).
|
|
371
|
-
*/
|
|
357
|
+
/** Cache a batch's results in the shared store under its batchId. */
|
|
372
358
|
_cacheBatchResult(batchId, results, mode) {
|
|
373
|
-
this.
|
|
374
|
-
while (this.batchResults.size > this.maxCachedBatches) {
|
|
375
|
-
const oldestKey = this.batchResults.keys().next().value;
|
|
376
|
-
this.batchResults.delete(oldestKey);
|
|
377
|
-
}
|
|
359
|
+
this.resultStore.put('batch_scrape', { results, mode }, { key: batchId });
|
|
378
360
|
}
|
|
379
361
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
if (now - entry.timestamp >= entry.ttl) this.batchResults.delete(id);
|
|
384
|
-
}
|
|
362
|
+
/** Live store entries this tool wrote. */
|
|
363
|
+
_cachedBatchIds() {
|
|
364
|
+
return this.resultStore.list().filter((entry) => entry.toolName === 'batch_scrape').map((entry) => entry.handle);
|
|
385
365
|
}
|
|
386
366
|
|
|
387
367
|
_updateAverageBatchTime(batchTime) {
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* read_result — read a stored result by its handle (Phase 2).
|
|
3
|
+
*
|
|
4
|
+
* A tool whose result passed max_inline_chars returned a preview and a
|
|
5
|
+
* result_handle; the whole result is in the ResultStore for an hour. This
|
|
6
|
+
* tool slices, searches, paginates by line or reads a JSON path out of it,
|
|
7
|
+
* so the caller never has to fetch the page again. Extractive only: every
|
|
8
|
+
* operation returns verbatim text with offsets into the same view the
|
|
9
|
+
* preview was cut from (G3).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import { selectJsonPath } from 'crawlforge-extractors';
|
|
14
|
+
import { getResultStore, RESULT_HANDLE_PATTERN } from '../../core/ResultStore.js';
|
|
15
|
+
import { MAX_INLINE_CHARS_PARAM, resolveMaxInlineChars, resultTextView } from '../../server/inlineThreshold.js';
|
|
16
|
+
import { dualOutput } from '../../server/registerTool.js';
|
|
17
|
+
|
|
18
|
+
const CONTEXT_CHARS = 200;
|
|
19
|
+
const DEFAULT_SLICE_LENGTH = 10000;
|
|
20
|
+
const DEFAULT_LINE_COUNT = 200;
|
|
21
|
+
const MAX_LINE_COUNT = 5000;
|
|
22
|
+
|
|
23
|
+
export const READ_RESULT_INPUT_SHAPE = {
|
|
24
|
+
handle: z.string().regex(RESULT_HANDLE_PATTERN).describe("The result_handle a truncated result returned (res_… or a batch id)"),
|
|
25
|
+
operation: z.enum(['slice', 'search', 'lines', 'json_path']).describe("slice: characters from offset; search: case-insensitive literal query with context and offsets; lines: a page of lines; json_path: one subtree of a JSON result"),
|
|
26
|
+
offset: z.number().int().min(0).optional().describe("slice: first character (default 0); lines: first line index (default 0)"),
|
|
27
|
+
length: z.number().int().min(1).max(200000).optional().describe("slice: characters to return (default 10,000); lines: lines to return (default 200, max 5,000)"),
|
|
28
|
+
query: z.string().min(1).max(500).optional().describe("search: the text to find, matched literally, case-insensitive"),
|
|
29
|
+
max_matches: z.number().int().min(1).max(100).optional().default(20).describe("search: matches to return (default 20)"),
|
|
30
|
+
path: z.string().optional().describe("json_path: dotted keys and array indexes, e.g. \"results[3].content\" — not JSONPath"),
|
|
31
|
+
...MAX_INLINE_CHARS_PARAM
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const fail = (text) => ({ content: [{ type: 'text', text }], isError: true });
|
|
35
|
+
// read_result declares an outputSchema, so a success carries structuredContent too.
|
|
36
|
+
const ok = (object) => dualOutput(object);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {{ handle: string, operation: string, offset?: number, length?: number, query?: string, max_matches?: number, path?: string, max_inline_chars?: number }} params
|
|
40
|
+
*/
|
|
41
|
+
export async function readResultHandler(params) {
|
|
42
|
+
const { handle, operation } = params;
|
|
43
|
+
const entry = getResultStore().get(handle);
|
|
44
|
+
if (!entry) return fail('Unknown or expired result handle (results are kept 1 hour)');
|
|
45
|
+
|
|
46
|
+
const maxChars = resolveMaxInlineChars(params);
|
|
47
|
+
const { view, view_path, text } = resultTextView(entry.payload, entry.meta?.view_path ? [entry.meta.view_path] : []);
|
|
48
|
+
const base = {
|
|
49
|
+
handle,
|
|
50
|
+
tool: entry.toolName,
|
|
51
|
+
operation,
|
|
52
|
+
view,
|
|
53
|
+
view_path,
|
|
54
|
+
total_chars: text.length,
|
|
55
|
+
expires_at: new Date(entry.expiresAt).toISOString()
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
switch (operation) {
|
|
59
|
+
case 'slice': {
|
|
60
|
+
const offset = params.offset ?? 0;
|
|
61
|
+
const length = Math.min(params.length ?? DEFAULT_SLICE_LENGTH, maxChars);
|
|
62
|
+
const slice = text.slice(offset, offset + length);
|
|
63
|
+
return ok({ ...base, offset, length: slice.length, text: slice, has_more: offset + slice.length < text.length });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
case 'search': {
|
|
67
|
+
const { query } = params;
|
|
68
|
+
if (!query) return fail('query is required for operation "search"');
|
|
69
|
+
const maxMatches = params.max_matches ?? 20;
|
|
70
|
+
// A literal (escaped) case-insensitive regex over the ORIGINAL text:
|
|
71
|
+
// offsets must index the verbatim string, and lowercasing a copy can
|
|
72
|
+
// change its length (Turkish İ lowercases to two code units).
|
|
73
|
+
const literal = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
74
|
+
const matches = [];
|
|
75
|
+
let total = 0;
|
|
76
|
+
let budget = maxChars;
|
|
77
|
+
for (let m = literal.exec(text); m !== null; m = literal.exec(text)) {
|
|
78
|
+
if (m[0].length === 0) literal.lastIndex++;
|
|
79
|
+
total++;
|
|
80
|
+
if (matches.length >= maxMatches) continue;
|
|
81
|
+
const contextOffset = Math.max(0, m.index - CONTEXT_CHARS);
|
|
82
|
+
const context = text.slice(contextOffset, m.index + m[0].length + CONTEXT_CHARS);
|
|
83
|
+
if (context.length > budget) continue;
|
|
84
|
+
budget -= context.length;
|
|
85
|
+
matches.push({ offset: m.index, length: m[0].length, context_offset: contextOffset, context });
|
|
86
|
+
}
|
|
87
|
+
return ok({ ...base, query, matches, total_matches: total, truncated: total > matches.length });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
case 'lines': {
|
|
91
|
+
const all = text.split('\n');
|
|
92
|
+
const first = params.offset ?? 0;
|
|
93
|
+
const count = Math.min(params.length ?? DEFAULT_LINE_COUNT, MAX_LINE_COUNT);
|
|
94
|
+
let charOffset = 0;
|
|
95
|
+
for (let i = 0; i < Math.min(first, all.length); i++) charOffset += all[i].length + 1;
|
|
96
|
+
const lines = [];
|
|
97
|
+
let chars = 0;
|
|
98
|
+
for (const line of all.slice(first, first + count)) {
|
|
99
|
+
if (chars + line.length + 1 > maxChars && lines.length > 0) break;
|
|
100
|
+
lines.push(line);
|
|
101
|
+
chars += line.length + 1;
|
|
102
|
+
}
|
|
103
|
+
return ok({
|
|
104
|
+
...base,
|
|
105
|
+
first_line: first,
|
|
106
|
+
line_count: lines.length,
|
|
107
|
+
total_lines: all.length,
|
|
108
|
+
char_offset: charOffset,
|
|
109
|
+
lines,
|
|
110
|
+
has_more: first + lines.length < all.length
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case 'json_path': {
|
|
115
|
+
const { path } = params;
|
|
116
|
+
if (!path) return fail('path is required for operation "json_path"');
|
|
117
|
+
// A text view whose text is itself JSON (a fetch_url body) is read as
|
|
118
|
+
// that JSON; otherwise the stored result object is the subject.
|
|
119
|
+
let subject = entry.payload;
|
|
120
|
+
if (view === 'text') {
|
|
121
|
+
try { subject = JSON.parse(text); } catch { /* not JSON: address the result object */ }
|
|
122
|
+
}
|
|
123
|
+
let value;
|
|
124
|
+
try {
|
|
125
|
+
value = selectJsonPath(subject, path);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return fail(error.message);
|
|
128
|
+
}
|
|
129
|
+
const valueJson = JSON.stringify(value) ?? 'null';
|
|
130
|
+
if (valueJson.length > maxChars) {
|
|
131
|
+
return ok({
|
|
132
|
+
...base,
|
|
133
|
+
path,
|
|
134
|
+
value: null,
|
|
135
|
+
value_chars: valueJson.length,
|
|
136
|
+
preview: JSON.stringify(value, null, 2).slice(0, maxChars),
|
|
137
|
+
truncated: true,
|
|
138
|
+
warnings: [`Value at "${path}" is ${valueJson.length} chars, over max_inline_chars ${maxChars}; narrow the path to a smaller subtree.`]
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return ok({ ...base, path, value, value_chars: valueJson.length });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
default:
|
|
145
|
+
return fail(`Unknown operation "${operation}"`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* _highlights.js — the pure pieces of `scrape`'s query-scoped formats
|
|
3
|
+
* (Phase 1): the public unit shape, the parser for a model's excerpt
|
|
4
|
+
* choice, and the grounding check a model-mode answer must pass.
|
|
5
|
+
*
|
|
6
|
+
* Nothing here touches the network or a model, so every rule is testable
|
|
7
|
+
* without one.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The unit shape a caller sees. `heading` is a ranking input
|
|
12
|
+
* (crawlforge-extractors counts its terms at half weight), not an output.
|
|
13
|
+
* @param {{ text: string, kind: string, offset: number, length: number, score: number }} unit
|
|
14
|
+
*/
|
|
15
|
+
export function toPublicUnit({ text, kind, offset, length, score }) {
|
|
16
|
+
return { text, kind, offset, length, score };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The candidate indexes a model chose, from a reply such as "3, 0, 7" or
|
|
21
|
+
* "[3] and [7]". Out-of-range and repeated numbers are dropped; at most
|
|
22
|
+
* `limit` survive, in the model's order. Empty when nothing parses, so the
|
|
23
|
+
* caller can fall back to the extractive order.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} reply
|
|
26
|
+
* @param {number} count how many candidates were offered (indexes 0..count-1)
|
|
27
|
+
* @param {number} limit
|
|
28
|
+
* @returns {number[]}
|
|
29
|
+
*/
|
|
30
|
+
export function parseChosenIndexes(reply, count, limit) {
|
|
31
|
+
const chosen = [];
|
|
32
|
+
for (const match of String(reply ?? '').matchAll(/\d+/g)) {
|
|
33
|
+
const index = Number(match[0]);
|
|
34
|
+
if (index < count && !chosen.includes(index)) chosen.push(index);
|
|
35
|
+
if (chosen.length >= limit) break;
|
|
36
|
+
}
|
|
37
|
+
return chosen;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const NUMBER = /\d[\d.,:%]*/g;
|
|
41
|
+
// A capitalised word of two or more letters. Letters only: "Node.js" is
|
|
42
|
+
// "Node" (checked) and "js" (not capitalised).
|
|
43
|
+
const WORD = /\p{L}{2,}/gu;
|
|
44
|
+
// What precedes the first word of a sentence: nothing, or a terminator.
|
|
45
|
+
const SENTENCE_START = /(?:^|[.!?]|\n)[\s"'“‘(\[*_]*$/;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether a model-written answer stays inside its evidence. Every number
|
|
49
|
+
* and every proper noun in `answer` — a capitalised word of at least two
|
|
50
|
+
* letters that is not the first word of a sentence — must appear,
|
|
51
|
+
* case-insensitively, in the evidence or in the question. A price the page
|
|
52
|
+
* never states or a vendor it never names is what "grounded: false" is for.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} answer
|
|
55
|
+
* @param {string} evidence the evidence units' text
|
|
56
|
+
* @param {string} question
|
|
57
|
+
* @returns {{ grounded: boolean, unbacked: string[] }}
|
|
58
|
+
*/
|
|
59
|
+
export function groundingCheck(answer, evidence, question) {
|
|
60
|
+
const text = String(answer ?? '');
|
|
61
|
+
const haystack = `${evidence ?? ''}\n${question ?? ''}`.toLowerCase();
|
|
62
|
+
const unbacked = [];
|
|
63
|
+
const check = (token) => {
|
|
64
|
+
if (!haystack.includes(token.toLowerCase()) && !unbacked.includes(token)) unbacked.push(token);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
for (const match of text.matchAll(NUMBER)) {
|
|
68
|
+
// "$49." at the end of a sentence is the number 49.
|
|
69
|
+
check(match[0].replace(/[.,:]+$/, ''));
|
|
70
|
+
}
|
|
71
|
+
for (const match of text.matchAll(WORD)) {
|
|
72
|
+
const word = match[0];
|
|
73
|
+
if (word[0] !== word[0].toUpperCase() || word[0] === word[0].toLowerCase()) continue;
|
|
74
|
+
if (SENTENCE_START.test(text.slice(0, match.index))) continue;
|
|
75
|
+
check(word);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { grounded: unbacked.length === 0, unbacked };
|
|
79
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* escalation.js — the one declaration of `scrape`'s opt-in auto-escalation.
|
|
3
|
+
*
|
|
4
|
+
* `escalate: true` asks the tool to run the stealth browser itself when the
|
|
5
|
+
* plain fetch comes back walled (Phase 3). The plain fetch always runs first
|
|
6
|
+
* (G1: never start with stealth), so the escalation is a second stage, not a
|
|
7
|
+
* different tool: it reuses the same compliance gate and the same
|
|
8
|
+
* StealthBrowserManager `stealth_mode` uses, and adds no new evasion.
|
|
9
|
+
*
|
|
10
|
+
* The two fields and the price rule live here so `server.js`, the tool
|
|
11
|
+
* module, `AuthManager.getToolCost` and the output schema read one
|
|
12
|
+
* declaration (G5) — the same arrangement `formats.js` already has for the
|
|
13
|
+
* query-format surcharge.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
|
|
18
|
+
/** What an escalated call adds to `scrape`'s price: the stealth browser's own. */
|
|
19
|
+
export const SCRAPE_ESCALATION_CREDITS = 5;
|
|
20
|
+
|
|
21
|
+
/** The two public fields, in tools/list order, with the text the client sees. */
|
|
22
|
+
export const SCRAPE_ESCALATION_SHAPE = {
|
|
23
|
+
escalate: z.boolean().optional().default(false).describe('When the plain fetch comes back blocked (403/429/challenge page/empty shell), retry once in the stealth browser and return its content instead of the block. Projected at 2+5; the actual charge stays at the base price when the plain fetch succeeded. Default: false'),
|
|
24
|
+
escalate_engine: z.enum(['playwright', 'camoufox']).optional().default('playwright').describe('Stealth engine for the escalated retry (default: "playwright")')
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What escalation adds to `scrape`'s base price: 5 when the caller asked for
|
|
29
|
+
* it, nothing otherwise. Reads the RAW param — getToolCost runs before
|
|
30
|
+
* validation, so anything that is not exactly `true` prices as 0.
|
|
31
|
+
*
|
|
32
|
+
* @param {unknown} escalate the raw `escalate` param
|
|
33
|
+
* @returns {0 | 5}
|
|
34
|
+
*/
|
|
35
|
+
export function scrapeEscalationSurcharge(escalate) {
|
|
36
|
+
return escalate === true ? SCRAPE_ESCALATION_CREDITS : 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether a second, browser-backed attempt could plausibly change this
|
|
41
|
+
* outcome.
|
|
42
|
+
*
|
|
43
|
+
* A named vendor's wall qualifies, and so does a bare 403 or 429 — an
|
|
44
|
+
* IP-reputation, WAF or User-Agent block, which a real browser and fingerprint
|
|
45
|
+
* often do pass (travel.state.gov answers some networks with exactly that). So
|
|
46
|
+
* does any failure that arrived with a 2xx: an empty shell, a JavaScript-only
|
|
47
|
+
* document or a soft-error placeholder is precisely what rendering fixes.
|
|
48
|
+
*
|
|
49
|
+
* A 404 is not a wall and a 5xx is the server's own failure. Escalating either
|
|
50
|
+
* would spend the browser's 5 credits to be told the same thing, so `escalate`
|
|
51
|
+
* leaves them as the plain fetch reported them (G4 — never charge for work
|
|
52
|
+
* that cannot help).
|
|
53
|
+
*
|
|
54
|
+
* @param {{ blocked?: unknown } | null | undefined} verdict
|
|
55
|
+
* @param {number | null | undefined} status
|
|
56
|
+
* @returns {boolean}
|
|
57
|
+
*/
|
|
58
|
+
export function aBrowserMightPass(verdict, status) {
|
|
59
|
+
if (verdict && verdict.blocked) return true;
|
|
60
|
+
if (status === 403 || status === 429) return true;
|
|
61
|
+
return status === null || status === undefined || (status >= 200 && status < 300);
|
|
62
|
+
}
|
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
* toolOutputSchemas.js separately, so a format added to one copy shipped a
|
|
6
6
|
* schema that did not describe the tool (Phase 0, 0.3). server.js and the
|
|
7
7
|
* output schema import from here; the tool module re-exports.
|
|
8
|
+
*
|
|
9
|
+
* The query-scoped formats (Phase 1) carry their price rule here too, so
|
|
10
|
+
* AuthManager.getToolCost and the tool read the same one.
|
|
8
11
|
*/
|
|
9
12
|
|
|
10
13
|
import { z } from 'zod';
|
|
@@ -17,7 +20,50 @@ export const JsonFormatSchema = z.object({
|
|
|
17
20
|
prompt: z.string().optional().describe('Extraction instruction for the LLM')
|
|
18
21
|
});
|
|
19
22
|
|
|
23
|
+
// "extractive" returns page text verbatim and calls no model; "model" lets a
|
|
24
|
+
// model choose among (highlights) or answer from (question) the extractive
|
|
25
|
+
// units, and is priced separately.
|
|
26
|
+
const QueryModeSchema = z.enum(['extractive', 'model']).optional().default('extractive')
|
|
27
|
+
.describe('"extractive" (default) returns verbatim page text, no model; "model" adds an LLM step (+3 credits)');
|
|
28
|
+
|
|
29
|
+
export const HighlightsFormatSchema = z.object({
|
|
30
|
+
type: z.literal('highlights'),
|
|
31
|
+
query: z.string().min(1).max(500).describe('What to look for; the matching sentences, table rows and code blocks come back verbatim with offsets into the markdown'),
|
|
32
|
+
max_highlights: z.number().int().min(1).max(50).optional().default(10).describe('How many units to return (default 10)'),
|
|
33
|
+
mode: QueryModeSchema
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const QuestionFormatSchema = z.object({
|
|
37
|
+
type: z.literal('question'),
|
|
38
|
+
question: z.string().min(1).max(500).describe('The question to answer from the page; the evidence units come back verbatim with offsets'),
|
|
39
|
+
mode: QueryModeSchema
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const OBJECT_FORMAT_SCHEMAS = [JsonFormatSchema, HighlightsFormatSchema, QuestionFormatSchema];
|
|
43
|
+
|
|
44
|
+
/** The `type` of each object format, in schema order. */
|
|
45
|
+
export const SCRAPE_OBJECT_FORMATS = OBJECT_FORMAT_SCHEMAS.map((schema) => schema.shape.type.value);
|
|
46
|
+
|
|
20
47
|
export const FormatSchema = z.union([
|
|
21
48
|
z.enum(SCRAPE_STRING_FORMATS),
|
|
22
|
-
|
|
49
|
+
...OBJECT_FORMAT_SCHEMAS
|
|
23
50
|
]);
|
|
51
|
+
|
|
52
|
+
const QUERY_FORMATS = new Set(['highlights', 'question']);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What the query-scoped formats add to `scrape`'s base price: 1 credit once
|
|
56
|
+
* per call when any `highlights` or `question` format is present, 3 more
|
|
57
|
+
* once when any of them asks for `mode: "model"`. Reads the raw params
|
|
58
|
+
* (getToolCost runs before validation), so anything that is not an array
|
|
59
|
+
* of formats prices as none.
|
|
60
|
+
*
|
|
61
|
+
* @param {unknown} formats
|
|
62
|
+
* @returns {{ query: 0 | 1, model: 0 | 3 }}
|
|
63
|
+
*/
|
|
64
|
+
export function scrapeFormatSurcharge(formats) {
|
|
65
|
+
if (!Array.isArray(formats)) return { query: 0, model: 0 };
|
|
66
|
+
const queryFormats = formats.filter((fmt) => fmt && typeof fmt === 'object' && QUERY_FORMATS.has(fmt.type));
|
|
67
|
+
if (queryFormats.length === 0) return { query: 0, model: 0 };
|
|
68
|
+
return { query: 1, model: queryFormats.some((fmt) => fmt.mode === 'model') ? 3 : 0 };
|
|
69
|
+
}
|