@howells/fieldwork-sdk 0.5.2 → 0.6.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/README.md +25 -24
- package/dist/{adapter-BhTK51Yj.d.ts → adapter-Bh8EIrNe.d.ts} +1 -1
- package/dist/brave.d.ts +1 -1
- package/dist/brave.js +3 -1
- package/dist/browserbase.d.ts +7 -2
- package/dist/browserbase.js +5 -3
- package/dist/{document-CjMeBtV5.js → document-Clmi51Sf.js} +4 -1
- package/dist/exa.d.ts +2 -2
- package/dist/exa.js +21 -12
- package/dist/{fieldwork-CseCD_-H.d.ts → fieldwork-BaWqUNI3.d.ts} +1 -1
- package/dist/firecrawl.d.ts +2 -2
- package/dist/firecrawl.js +76 -52
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/job-CWHOfhP_.js +102 -0
- package/dist/kernel.d.ts +99 -0
- package/dist/kernel.js +313 -0
- package/dist/llamaparse.d.ts +1 -1
- package/dist/llamaparse.js +18 -11
- package/dist/parallel.d.ts +8 -4
- package/dist/parallel.js +72 -27
- package/dist/{parse-BY3R0oWO.js → parse-COHKvfH9.js} +61 -8
- package/dist/perplexity.d.ts +2 -2
- package/dist/perplexity.js +58 -38
- package/dist/reducto.d.ts +1 -1
- package/dist/reducto.js +18 -11
- package/dist/{schema-CNd51xdD.d.ts → schema-BWWiMvkg.d.ts} +1 -1
- package/dist/serpapi.d.ts +1 -1
- package/dist/serpapi.js +1 -1
- package/dist/tavily.d.ts +2 -2
- package/dist/tavily.js +18 -11
- package/dist/tools.d.ts +2 -2
- package/dist/tools.js +22 -3
- package/package.json +9 -2
- package/dist/job-DVsmQMKP.js +0 -47
package/dist/kernel.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { R as Page, a as RawClient, d as BrowserSession, p as CallOptions, t as Adapter } from "./adapter-Bh8EIrNe.js";
|
|
2
|
+
/** Settings for `kernel()`. */
|
|
3
|
+
export interface KernelConfig {
|
|
4
|
+
/** Defaults to the KERNEL_API_KEY environment variable. */
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
/** Defaults to https://api.onkernel.com. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
/** Defaults to the global `fetch`. A test passes a fake. */
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
11
|
+
retries?: number;
|
|
12
|
+
}
|
|
13
|
+
/** A saved browser profile (cookies, storage), by id or by name. */
|
|
14
|
+
export type KernelProfile = {
|
|
15
|
+
id: string;
|
|
16
|
+
name?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
name: string;
|
|
19
|
+
id?: undefined;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Where the browser's traffic leaves: a proxy of the project, by id or name,
|
|
23
|
+
* or `direct` for no proxy. Without it, a stealth browser uses Kernel's
|
|
24
|
+
* stealth proxy and any other browser goes direct.
|
|
25
|
+
*/
|
|
26
|
+
export type KernelProxy = {
|
|
27
|
+
id: string;
|
|
28
|
+
name?: undefined;
|
|
29
|
+
mode?: undefined;
|
|
30
|
+
} | {
|
|
31
|
+
name: string;
|
|
32
|
+
id?: undefined;
|
|
33
|
+
mode?: undefined;
|
|
34
|
+
} | {
|
|
35
|
+
mode: "direct";
|
|
36
|
+
id?: undefined;
|
|
37
|
+
name?: undefined;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Options for `read`, which loads an http or https URL in a new headless
|
|
41
|
+
* browser, waits for it to render and deletes the browser. The session bills
|
|
42
|
+
* for the seconds it ran, reported as `meta.usage.units.uptime_ms`. When the
|
|
43
|
+
* browser cannot be deleted, the page still comes back, without the billed
|
|
44
|
+
* time, and `meta.warnings` names the browser, which bills until its
|
|
45
|
+
* 60-second idle timeout. Throws `InvalidRequest`, before creating a browser,
|
|
46
|
+
* for a URL that is not http or https.
|
|
47
|
+
*/
|
|
48
|
+
export interface KernelReadOptions extends CallOptions {
|
|
49
|
+
/**
|
|
50
|
+
* "markdown" converts the rendered page to Markdown (`Page.markdown`),
|
|
51
|
+
* leaving out the nav, header, footer and aside elements that are not
|
|
52
|
+
* inside a main or article element; a page with neither loses all four.
|
|
53
|
+
* "raw" returns the rendered HTML (`Page.rawHtml`). Defaults to "markdown".
|
|
54
|
+
*/
|
|
55
|
+
format?: "markdown" | "raw";
|
|
56
|
+
/** Kernel's stealth mode and CAPTCHA solver, for sites that block automated browsers. */
|
|
57
|
+
stealth?: boolean;
|
|
58
|
+
/** Load a saved profile's cookies and storage. The read never saves to it. */
|
|
59
|
+
profile?: KernelProfile;
|
|
60
|
+
proxy?: KernelProxy;
|
|
61
|
+
}
|
|
62
|
+
/** Options for `browser.open`. The session bills while it runs, until `browser.close` or its idle timeout. */
|
|
63
|
+
export interface KernelOpenOptions extends CallOptions {
|
|
64
|
+
/** A browser without a screen: cheaper, but with no live view. Kernel defaults to false. */
|
|
65
|
+
headless?: boolean;
|
|
66
|
+
/** Kernel's stealth mode and CAPTCHA solver. */
|
|
67
|
+
stealth?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Seconds without a CDP or live-view connection before Kernel closes the
|
|
70
|
+
* session, 10 to 259200. Kernel defaults to 60.
|
|
71
|
+
*/
|
|
72
|
+
idleTimeout?: number;
|
|
73
|
+
/** Load a saved profile; `saveChanges` writes this session's state back to it when the session ends. */
|
|
74
|
+
profile?: KernelProfile & {
|
|
75
|
+
saveChanges?: boolean;
|
|
76
|
+
};
|
|
77
|
+
proxy?: KernelProxy;
|
|
78
|
+
viewport?: {
|
|
79
|
+
width: number;
|
|
80
|
+
height: number;
|
|
81
|
+
};
|
|
82
|
+
/** Start-Up and Enterprise plans only. Kernel defaults to us-east. */
|
|
83
|
+
region?: "us-east" | "us-west" | "eu-west" | "ap-southeast";
|
|
84
|
+
}
|
|
85
|
+
/** What the Kernel adapter offers. */
|
|
86
|
+
export interface KernelOperations {
|
|
87
|
+
read: (url: string, options?: KernelReadOptions) => Promise<Page>;
|
|
88
|
+
browser: {
|
|
89
|
+
open: (options?: KernelOpenOptions) => Promise<BrowserSession>;
|
|
90
|
+
/** Deletes the session, so it stops billing. */
|
|
91
|
+
close: (id: string, options?: CallOptions) => Promise<void>;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* An adapter for Kernel: read and browser. Pass it to `new Fieldwork({ adapter })`.
|
|
96
|
+
*
|
|
97
|
+
* Throws `Unauthorized` when neither `apiKey` nor `KERNEL_API_KEY` is set.
|
|
98
|
+
*/
|
|
99
|
+
export declare const kernel: (config?: KernelConfig) => Adapter<"kernel", KernelOperations, RawClient>;
|
package/dist/kernel.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
3
|
+
import { B as requireJobId, C as createRawClient, E as segment, I as presentPage, M as compact, P as meta, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, k as callSettings, l as optionalBoolean, n as anything, p as optionalObject, s as optionalAnyString } from "./parse-COHKvfH9.js";
|
|
4
|
+
import { r as runJob } from "./job-CWHOfhP_.js";
|
|
5
|
+
import * as z from "zod/mini";
|
|
6
|
+
import TurndownService from "turndown";
|
|
7
|
+
// src/adapters/kernel/response.ts
|
|
8
|
+
/** `POST /browsers`. The id keeps the value as it arrived, for the error raised when there is none. */
|
|
9
|
+
const BrowserResponse = looseObject({
|
|
10
|
+
browser_live_view_url: optionalString,
|
|
11
|
+
cdp_ws_url: optionalString,
|
|
12
|
+
session_id: withOriginal(optionalId)
|
|
13
|
+
});
|
|
14
|
+
/** `GET /browsers/{id}?include_deleted=true` after deletion: the billed time, once `usage_status` is ready. */
|
|
15
|
+
const DeletedBrowserResponse = looseObject({
|
|
16
|
+
usage: optionalObject({ uptime_ms: optionalNumber }),
|
|
17
|
+
usage_status: optionalString
|
|
18
|
+
});
|
|
19
|
+
/** `POST /browsers/{id}/playwright/execute`. `result` is whatever the script returned. */
|
|
20
|
+
const ExecuteResponse = looseObject({
|
|
21
|
+
error: optionalString,
|
|
22
|
+
result: anything,
|
|
23
|
+
stderr: optionalAnyString,
|
|
24
|
+
success: optionalBoolean
|
|
25
|
+
});
|
|
26
|
+
/** What the read script returns: the page after it rendered. */
|
|
27
|
+
const RenderedPage = looseObject({
|
|
28
|
+
html: optionalAnyString,
|
|
29
|
+
status: optionalNumber,
|
|
30
|
+
title: optionalString,
|
|
31
|
+
url: optionalString
|
|
32
|
+
});
|
|
33
|
+
// src/adapters/kernel.ts
|
|
34
|
+
/** Seconds Kernel gives the read script, within its 300-second limit. */
|
|
35
|
+
const READ_SCRIPT_SECONDS = 120;
|
|
36
|
+
/** Seconds a read's session may sit idle before Kernel deletes it. */
|
|
37
|
+
const READ_IDLE_SECONDS = 60;
|
|
38
|
+
/** Milliseconds a read waits for Kernel to delete its session, apart from the call's own deadline. */
|
|
39
|
+
const RELEASE_TIMEOUT = 1e4;
|
|
40
|
+
/** Milliseconds a read waits for the deleted session's billed time. */
|
|
41
|
+
const BILLED_TIME_WAIT = 15e3;
|
|
42
|
+
/** The elements that hold a page's content, inside which site chrome is kept. */
|
|
43
|
+
const CONTENT = /* @__PURE__ */ new Set(["main", "article"]);
|
|
44
|
+
/** Site chrome: removed from the Markdown unless it is inside a main or article element. */
|
|
45
|
+
const CHROME = /* @__PURE__ */ new Set([
|
|
46
|
+
"nav",
|
|
47
|
+
"header",
|
|
48
|
+
"footer",
|
|
49
|
+
"aside"
|
|
50
|
+
]);
|
|
51
|
+
/**
|
|
52
|
+
* The Playwright script `read` runs in the browser. It waits for the DOM and
|
|
53
|
+
* then up to 10 seconds for the network to go quiet: pages that poll or
|
|
54
|
+
* stream never do, and their content has rendered by then.
|
|
55
|
+
*/
|
|
56
|
+
const readScript = (url) => `
|
|
57
|
+
const response = await page.goto(${JSON.stringify(url)}, { waitUntil: "domcontentloaded", timeout: 90000 });
|
|
58
|
+
await page.waitForLoadState("networkidle", { timeout: 10000 }).catch(() => undefined);
|
|
59
|
+
return { html: await page.content(), status: response?.status() ?? null, title: await page.title(), url: page.url() };
|
|
60
|
+
`;
|
|
61
|
+
/** Whether the node has a main or article element among its ancestors. */
|
|
62
|
+
const inContent = (node) => node.parentNode !== null && (CONTENT.has(node.parentNode.nodeName.toLowerCase()) || inContent(node.parentNode));
|
|
63
|
+
/**
|
|
64
|
+
* The rendered page as Markdown, without the site's navigation, header,
|
|
65
|
+
* footer and sidebars, which on a retailer's page can be most of the text.
|
|
66
|
+
* Those elements are kept inside a main or article element, where they are
|
|
67
|
+
* the content's own; a page with neither loses all of them.
|
|
68
|
+
*/
|
|
69
|
+
const markdownOf = (html) => {
|
|
70
|
+
const service = new TurndownService({
|
|
71
|
+
bulletListMarker: "-",
|
|
72
|
+
codeBlockStyle: "fenced",
|
|
73
|
+
headingStyle: "atx"
|
|
74
|
+
});
|
|
75
|
+
service.remove([
|
|
76
|
+
"title",
|
|
77
|
+
"script",
|
|
78
|
+
"style",
|
|
79
|
+
"noscript",
|
|
80
|
+
"template",
|
|
81
|
+
"iframe"
|
|
82
|
+
]);
|
|
83
|
+
service.remove((node) => CHROME.has(node.nodeName.toLowerCase()) && !inContent(node));
|
|
84
|
+
return service.turndown(html).trim();
|
|
85
|
+
};
|
|
86
|
+
/** The session's profile and proxy as Kernel's create body spells them. */
|
|
87
|
+
const sessionBody = (o) => ({
|
|
88
|
+
profile: o.profile ? compact({
|
|
89
|
+
id: o.profile.id,
|
|
90
|
+
name: o.profile.name,
|
|
91
|
+
save_changes: o.profile.saveChanges
|
|
92
|
+
}) : void 0,
|
|
93
|
+
proxy: o.proxy ? compact({
|
|
94
|
+
id: o.proxy.id,
|
|
95
|
+
mode: o.proxy.mode,
|
|
96
|
+
name: o.proxy.name
|
|
97
|
+
}) : void 0
|
|
98
|
+
});
|
|
99
|
+
/** Why a session still bills, for a warning or an error. */
|
|
100
|
+
const leakText = (id, failure) => `could not delete browser ${id}, which bills until its ${READ_IDLE_SECONDS}-second idle timeout: ${failure.message}`;
|
|
101
|
+
/**
|
|
102
|
+
* Throws `InvalidRequest` unless the URL parses and is http or https, so no
|
|
103
|
+
* session is created for a URL the browser should not open.
|
|
104
|
+
*/
|
|
105
|
+
const checkUrl = (provider, url) => {
|
|
106
|
+
const parsed = URL.parse(url);
|
|
107
|
+
if (parsed === null) throw invalidRequest(provider, `cannot read ${url}: it is not a URL`);
|
|
108
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw invalidRequest(provider, `reads only http and https URLs, not ${parsed.protocol} (${url})`);
|
|
109
|
+
};
|
|
110
|
+
/** What the work returned, or what it threw. */
|
|
111
|
+
const settle = async (work) => {
|
|
112
|
+
try {
|
|
113
|
+
return {
|
|
114
|
+
ok: true,
|
|
115
|
+
value: await work
|
|
116
|
+
};
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return {
|
|
119
|
+
error,
|
|
120
|
+
ok: false
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Deletes a read's session under its own 10-second deadline, not the call's,
|
|
126
|
+
* so a read its caller stopped still stops the bill. A 404 or 410 means the
|
|
127
|
+
* session is already gone. Returns why the delete failed, or undefined when
|
|
128
|
+
* it did not, so the caller reports it without losing its own error.
|
|
129
|
+
*/
|
|
130
|
+
const release = async (raw, id, retries) => {
|
|
131
|
+
const deleted = await settle(raw.request("DELETE", `/browsers/${segment(id)}`, {
|
|
132
|
+
retries,
|
|
133
|
+
timeout: RELEASE_TIMEOUT
|
|
134
|
+
}));
|
|
135
|
+
if (deleted.ok) return;
|
|
136
|
+
const { error } = deleted;
|
|
137
|
+
if (!(error instanceof FieldworkError)) return new FieldworkError("ProviderError", String(error), {
|
|
138
|
+
cause: error,
|
|
139
|
+
provider: raw.provider
|
|
140
|
+
});
|
|
141
|
+
return error.status === 404 || error.status === 410 ? void 0 : error;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* The deleted session's billed time, which Kernel reports about a second after
|
|
145
|
+
* deletion, waiting up to 15 seconds and no longer than the call's signal
|
|
146
|
+
* allows. A failure to get it does not fail the read: the page is already
|
|
147
|
+
* read and billed, so the result carries a warning instead.
|
|
148
|
+
*/
|
|
149
|
+
const billedTime = async (raw, id, call) => {
|
|
150
|
+
try {
|
|
151
|
+
return { uptimeMs: await runJob("kernel", {
|
|
152
|
+
check: async (sessionId, request) => {
|
|
153
|
+
const body = z.parse(DeletedBrowserResponse, await raw.request("GET", `/browsers/${segment(sessionId)}`, {
|
|
154
|
+
...request,
|
|
155
|
+
query: { include_deleted: true }
|
|
156
|
+
}));
|
|
157
|
+
return body.usage_status === "ready" ? body : void 0;
|
|
158
|
+
},
|
|
159
|
+
finish: (body) => body.usage?.uptime_ms,
|
|
160
|
+
start: async () => id
|
|
161
|
+
}, {
|
|
162
|
+
interval: 500,
|
|
163
|
+
retries: call.retries,
|
|
164
|
+
signal: call.signal,
|
|
165
|
+
timeout: BILLED_TIME_WAIT
|
|
166
|
+
}) };
|
|
167
|
+
} catch (error) {
|
|
168
|
+
return { warning: error instanceof FieldworkError && error.code === "Timeout" && error.status === void 0 && call.signal?.aborted !== true ? `kernel did not report the session's billed time within ${BILLED_TIME_WAIT / 1e3} seconds` : `could not get the session's billed time: ${error instanceof Error ? error.message : String(error)}` };
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Runs the read script in the session and returns the rendered page with its
|
|
173
|
+
* HTML and Kernel's reply. Throws `ProviderError` when the script failed or
|
|
174
|
+
* returned no HTML.
|
|
175
|
+
*/
|
|
176
|
+
const render = async (raw, id, url, call) => {
|
|
177
|
+
const { provider } = raw;
|
|
178
|
+
const response = await raw.request("POST", `/browsers/${segment(id)}/playwright/execute`, {
|
|
179
|
+
...call,
|
|
180
|
+
body: {
|
|
181
|
+
code: readScript(url),
|
|
182
|
+
timeout_sec: READ_SCRIPT_SECONDS
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const body = z.parse(ExecuteResponse, response);
|
|
186
|
+
if (body.success !== true) throw new FieldworkError("ProviderError", `kernel could not load ${url}: ${body.error ?? "the script failed without an error"}`, {
|
|
187
|
+
cause: response,
|
|
188
|
+
provider
|
|
189
|
+
});
|
|
190
|
+
const page = z.parse(RenderedPage, body.result);
|
|
191
|
+
if (page.html === void 0) throw new FieldworkError("ProviderError", `kernel loaded ${url} but the script returned no HTML`, {
|
|
192
|
+
cause: response,
|
|
193
|
+
provider
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
html: page.html,
|
|
197
|
+
page,
|
|
198
|
+
response
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Loads the page in a new session, deletes the session whether or not the
|
|
203
|
+
* script succeeded, and builds the result. A failed read whose session could
|
|
204
|
+
* not be deleted fails with its own error, naming the session that still bills.
|
|
205
|
+
*/
|
|
206
|
+
const readPage = async (raw, url, options) => {
|
|
207
|
+
const { provider } = raw;
|
|
208
|
+
const { call, rest: o } = callSettings(options);
|
|
209
|
+
checkUrl(provider, url);
|
|
210
|
+
const format = o.format ?? "markdown";
|
|
211
|
+
const created = await raw.request("POST", "/browsers", {
|
|
212
|
+
creates: true,
|
|
213
|
+
...call,
|
|
214
|
+
body: compact({
|
|
215
|
+
headless: true,
|
|
216
|
+
stealth: o.stealth,
|
|
217
|
+
timeout_seconds: READ_IDLE_SECONDS,
|
|
218
|
+
...sessionBody({
|
|
219
|
+
profile: o.profile,
|
|
220
|
+
proxy: o.proxy
|
|
221
|
+
})
|
|
222
|
+
})
|
|
223
|
+
});
|
|
224
|
+
const id = requireJobId(provider, z.parse(BrowserResponse, created).session_id);
|
|
225
|
+
const rendered = await settle(render(raw, id, url, call));
|
|
226
|
+
const unreleased = await release(raw, id, call.retries);
|
|
227
|
+
if (!rendered.ok) {
|
|
228
|
+
const { error } = rendered;
|
|
229
|
+
if (unreleased === void 0) throw error;
|
|
230
|
+
const message = `${error instanceof Error ? error.message : String(error)}; also ${leakText(id, unreleased)}`;
|
|
231
|
+
throw error instanceof FieldworkError ? new FieldworkError(error.code, message, {
|
|
232
|
+
cause: error,
|
|
233
|
+
jobId: error.jobId,
|
|
234
|
+
provider: error.provider,
|
|
235
|
+
retryAfter: error.retryAfter,
|
|
236
|
+
retryable: error.retryable,
|
|
237
|
+
status: error.status
|
|
238
|
+
}) : new FieldworkError("ProviderError", message, {
|
|
239
|
+
cause: error,
|
|
240
|
+
provider
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
const { html, page, response } = rendered.value;
|
|
244
|
+
const billed = unreleased === void 0 ? await billedTime(raw, id, call) : { warning: leakText(id, unreleased) };
|
|
245
|
+
const markdown = format === "markdown" ? markdownOf(html) : void 0;
|
|
246
|
+
return presentPage(provider, compact({
|
|
247
|
+
markdown,
|
|
248
|
+
meta: meta({
|
|
249
|
+
requestId: id,
|
|
250
|
+
units: { uptime_ms: billed.uptimeMs },
|
|
251
|
+
warnings: [billed.warning, markdown === "" ? `kernel rendered ${url} but it has no text to convert to Markdown` : void 0].filter((warning) => warning !== void 0)
|
|
252
|
+
}),
|
|
253
|
+
raw: {
|
|
254
|
+
browser: created,
|
|
255
|
+
execute: response
|
|
256
|
+
},
|
|
257
|
+
rawHtml: format === "raw" ? html : void 0,
|
|
258
|
+
statusCode: page.status,
|
|
259
|
+
title: page.title,
|
|
260
|
+
url: page.url ?? url
|
|
261
|
+
}));
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* An adapter for Kernel: read and browser. Pass it to `new Fieldwork({ adapter })`.
|
|
265
|
+
*
|
|
266
|
+
* Throws `Unauthorized` when neither `apiKey` nor `KERNEL_API_KEY` is set.
|
|
267
|
+
*/
|
|
268
|
+
const kernel = (config = {}) => {
|
|
269
|
+
const provider = "kernel";
|
|
270
|
+
const raw = createRawClient({
|
|
271
|
+
baseUrl: config.baseUrl ?? "https://api.onkernel.com",
|
|
272
|
+
fetch: config.fetch,
|
|
273
|
+
headers: { Authorization: `Bearer ${requireKey(provider, config.apiKey, "KERNEL_API_KEY")}` },
|
|
274
|
+
provider,
|
|
275
|
+
retries: config.retries
|
|
276
|
+
});
|
|
277
|
+
return defineAdapter({
|
|
278
|
+
name: provider,
|
|
279
|
+
operations: {
|
|
280
|
+
browser: {
|
|
281
|
+
async close(id, options) {
|
|
282
|
+
const { call } = callSettings(options);
|
|
283
|
+
await raw.request("DELETE", `/browsers/${segment(id)}`, call);
|
|
284
|
+
},
|
|
285
|
+
async open(options) {
|
|
286
|
+
const { call, rest: o } = callSettings(options);
|
|
287
|
+
const response = await raw.request("POST", "/browsers", {
|
|
288
|
+
creates: true,
|
|
289
|
+
...call,
|
|
290
|
+
body: compact({
|
|
291
|
+
headless: o.headless,
|
|
292
|
+
region: o.region,
|
|
293
|
+
stealth: o.stealth,
|
|
294
|
+
timeout_seconds: o.idleTimeout,
|
|
295
|
+
viewport: o.viewport,
|
|
296
|
+
...sessionBody(o)
|
|
297
|
+
})
|
|
298
|
+
});
|
|
299
|
+
const session = z.parse(BrowserResponse, response);
|
|
300
|
+
return compact({
|
|
301
|
+
cdpUrl: session.cdp_ws_url,
|
|
302
|
+
id: requireJobId(provider, session.session_id),
|
|
303
|
+
liveViewUrl: session.browser_live_view_url,
|
|
304
|
+
raw: response
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
read: async (url, options) => await readPage(raw, url, options)
|
|
309
|
+
},
|
|
310
|
+
raw
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
export { kernel };
|
package/dist/llamaparse.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as ParsedDocument, C as DocumentInput, a as RawClient, p as CallOptions, t as Adapter } from "./adapter-
|
|
1
|
+
import { B as ParsedDocument, C as DocumentInput, a as RawClient, p as CallOptions, t as Adapter } from "./adapter-Bh8EIrNe.js";
|
|
2
2
|
/** Settings for `llamaparse()`. */
|
|
3
3
|
export interface LlamaParseConfig {
|
|
4
4
|
/** Defaults to the LLAMA_CLOUD_API_KEY environment variable. */
|
package/dist/llamaparse.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
-
import { a as refuseLarger, n as documentPart, r as enclosing } from "./document-
|
|
2
|
+
import { a as refuseLarger, n as documentPart, r as enclosing } from "./document-Clmi51Sf.js";
|
|
3
3
|
import { t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { C as createRawClient, E as segment, F as parseJson, M as compact, P as meta, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, k as callSettings, m as optionalScalar, n as anything, r as arrayOf, s as optionalAnyString, w as readSigned } from "./parse-COHKvfH9.js";
|
|
5
|
+
import { i as startJob, r as runJob, t as jobState } from "./job-CWHOfhP_.js";
|
|
6
6
|
import * as z from "zod/mini";
|
|
7
7
|
// src/adapters/llamaparse/response.ts
|
|
8
8
|
/** A box in page points, from the page's top-left corner. */
|
|
@@ -164,21 +164,28 @@ const startParse = async (raw, document, o, call) => {
|
|
|
164
164
|
const form = new FormData();
|
|
165
165
|
form.set("configuration", JSON.stringify(configuration(o)));
|
|
166
166
|
form.set("file", documentPart(document), document.name);
|
|
167
|
-
|
|
168
|
-
creates: true,
|
|
167
|
+
return await startJob(raw, "/api/v2/parse/upload", {
|
|
169
168
|
...call,
|
|
170
169
|
form
|
|
171
|
-
}));
|
|
172
|
-
|
|
170
|
+
}, JobStart, (reply) => reply.id);
|
|
171
|
+
};
|
|
172
|
+
const JOB_STATES = {
|
|
173
|
+
done: ["COMPLETED"],
|
|
174
|
+
failed: ["FAILED", "CANCELLED"],
|
|
175
|
+
running: ["PENDING", "RUNNING"]
|
|
173
176
|
};
|
|
174
177
|
/** The result once the job has completed, undefined while it runs; throws `JobFailed` when LlamaParse failed or cancelled it. */
|
|
175
|
-
const finished = (found) => {
|
|
178
|
+
const finished = (id, found) => {
|
|
176
179
|
const { error_message: message, status } = found.value.job;
|
|
177
|
-
|
|
180
|
+
const state = jobState(provider, id, JOB_STATES, {
|
|
181
|
+
original: found.original,
|
|
182
|
+
value: status
|
|
183
|
+
});
|
|
184
|
+
if (state === "failed") throw new FieldworkError("JobFailed", `llamaparse job ${status?.toLowerCase()}${message === void 0 ? "" : `: ${message}`}`, {
|
|
178
185
|
cause: found.original,
|
|
179
186
|
provider
|
|
180
187
|
});
|
|
181
|
-
return
|
|
188
|
+
return state === "done" ? found : void 0;
|
|
182
189
|
};
|
|
183
190
|
/** The grounded items: one JSON line per page, each page's items with word and cell boxes. */
|
|
184
191
|
const groundedPages = async (found, options) => {
|
|
@@ -248,7 +255,7 @@ const parse = async (raw, document, options, fetchImpl) => {
|
|
|
248
255
|
...o.pageImages === true ? ["images_content_metadata"] : []
|
|
249
256
|
].join(",");
|
|
250
257
|
return await runJob(provider, {
|
|
251
|
-
check: async (id, request) => finished(z.parse(JobResult, await raw.request("GET", `/api/v2/parse/${segment(id)}`, {
|
|
258
|
+
check: async (id, request) => finished(id, z.parse(JobResult, await raw.request("GET", `/api/v2/parse/${segment(id)}`, {
|
|
252
259
|
...request,
|
|
253
260
|
query: { expand }
|
|
254
261
|
}))),
|
package/dist/parallel.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as Extraction, F as MonitorEvents, I as MonitorInfo, L as MonitorSchedule, R as Page, Y as SearchResponse, a as RawClient, b as Decisions, g as Condition, j as ImageSearch, l as Answer, p as CallOptions, q as Research, t as Adapter, x as Discovery, y as DecisionQuestion, z as PageBatch } from "./adapter-
|
|
2
|
-
import { t as OutputSchema } from "./schema-
|
|
1
|
+
import { E as Extraction, F as MonitorEvents, I as MonitorInfo, L as MonitorSchedule, R as Page, Y as SearchResponse, a as RawClient, b as Decisions, g as Condition, j as ImageSearch, l as Answer, p as CallOptions, q as Research, t as Adapter, x as Discovery, y as DecisionQuestion, z as PageBatch } from "./adapter-Bh8EIrNe.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-BWWiMvkg.js";
|
|
3
3
|
import "./util-DhYr1OS3.js";
|
|
4
4
|
/** Settings for `parallel()`. */
|
|
5
5
|
interface ParallelConfig {
|
|
@@ -100,8 +100,12 @@ interface ParallelReadOptions extends CallOptions {
|
|
|
100
100
|
*/
|
|
101
101
|
type ParallelReadManyOptions = ParallelReadOptions;
|
|
102
102
|
/**
|
|
103
|
-
* Options for `extract`: a Task run
|
|
104
|
-
*
|
|
103
|
+
* Options for `extract`: a Task run, polled to completion, whose input names
|
|
104
|
+
* the URL and whose `source_policy.include_domains` holds one entry, the URL's
|
|
105
|
+
* hostname and path with any trailing slash dropped. The scheme, credentials,
|
|
106
|
+
* port, query string and fragment are not sent there, so the run is scoped by
|
|
107
|
+
* hostname and path, not by the exact URL. `resume` waits for an earlier run
|
|
108
|
+
* instead of starting another.
|
|
105
109
|
*/
|
|
106
110
|
interface ParallelExtractOptions<T = unknown> extends CallOptions {
|
|
107
111
|
/**
|