@howells/fieldwork-sdk 0.4.0 → 0.5.1
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 +34 -31
- package/dist/{adapter-CDY8qczv.d.ts → adapter-BgdfLBs-.d.ts} +85 -3
- package/dist/adapter-DWYJys20.js +8 -0
- package/dist/brave.d.ts +6 -1
- package/dist/brave.js +34 -14
- package/dist/browserbase.d.ts +8 -4
- package/dist/browserbase.js +16 -14
- package/dist/document-Dpj01mGx.js +51 -0
- package/dist/{errors-BiIBG9zN.js → errors-Yvk42oZH.js} +3 -1
- package/dist/exa.d.ts +7 -2
- package/dist/exa.js +52 -43
- package/dist/{fieldwork-DTNVIOzp.d.ts → fieldwork-CXNIcqSN.d.ts} +7 -1
- package/dist/firecrawl.d.ts +7 -2
- package/dist/firecrawl.js +231 -173
- package/dist/index.d.ts +5 -4
- package/dist/index.js +10 -4
- package/dist/job-BGTwH3F-.js +41 -0
- package/dist/llamaparse.d.ts +58 -0
- package/dist/llamaparse.js +283 -0
- package/dist/parallel.d.ts +7 -2
- package/dist/parallel.js +95 -97
- package/dist/{parse-CxOri73y.js → parse-n8MmaK1x.js} +87 -50
- package/dist/perplexity.d.ts +16 -11
- package/dist/perplexity.js +164 -160
- package/dist/reducto.d.ts +31 -0
- package/dist/reducto.js +217 -0
- package/dist/{schema-EZlBkY5B.d.ts → schema-Cupy7-7q.d.ts} +1 -1
- package/dist/{schema-teOy7Od-.js → schema-DbSIABP0.js} +1 -1
- package/dist/serpapi.d.ts +6 -1
- package/dist/serpapi.js +17 -13
- package/dist/tavily.d.ts +7 -2
- package/dist/tavily.js +75 -69
- package/dist/tools.d.ts +5 -2
- package/dist/tools.js +62 -12
- package/package.json +14 -2
- package/dist/adapter-CEAQuT7F.js +0 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import { t as FieldworkError } from "./errors-
|
|
1
|
+
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
+
import { t as FieldworkError } from "./errors-Yvk42oZH.js";
|
|
3
3
|
import { n as GROUP_CAPABILITIES, t as CAPABILITIES } from "./types-CnomnF-6.js";
|
|
4
4
|
// src/fieldwork.ts
|
|
5
5
|
const bindOperation = (operations, capability) => {
|
|
@@ -28,7 +28,8 @@ var FieldworkClient = class {
|
|
|
28
28
|
this.provider = adapter.name;
|
|
29
29
|
this.raw = adapter.raw;
|
|
30
30
|
this.#providers = adapter.providers ?? {};
|
|
31
|
-
const
|
|
31
|
+
const bound = new Map(CAPABILITIES.map((capability) => [capability, bindOperation(adapter.operations, capability)]));
|
|
32
|
+
const has = (capability) => bound.get(capability) !== void 0;
|
|
32
33
|
this.capabilities = Object.freeze({
|
|
33
34
|
act: has("act"),
|
|
34
35
|
answer: has("answer"),
|
|
@@ -48,7 +49,7 @@ var FieldworkClient = class {
|
|
|
48
49
|
search: has("search"),
|
|
49
50
|
usage: has("usage")
|
|
50
51
|
});
|
|
51
|
-
Object.assign(this, Object.fromEntries(CAPABILITIES.map((capability) => [capability,
|
|
52
|
+
Object.assign(this, Object.fromEntries(CAPABILITIES.map((capability) => [capability, bound.get(capability) ?? unsupported(adapter.name, capability)])));
|
|
52
53
|
}
|
|
53
54
|
supports(capability) {
|
|
54
55
|
return this.capabilities[capability];
|
|
@@ -58,6 +59,11 @@ var FieldworkClient = class {
|
|
|
58
59
|
return this.#providers[capability] ?? this.provider;
|
|
59
60
|
}
|
|
60
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* The client: `new Fieldwork({ adapter })` has exactly the methods its adapter offers.
|
|
64
|
+
*
|
|
65
|
+
* Check `supports` or `capabilities` before calling dynamically; a capability the provider lacks throws `Unsupported`.
|
|
66
|
+
*/
|
|
61
67
|
const Fieldwork = FieldworkClient;
|
|
62
68
|
// src/router.ts
|
|
63
69
|
const build = (routes) => {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { i as withJob, t as FieldworkError } from "./errors-Yvk42oZH.js";
|
|
2
|
+
import { D as sleep, O as stopped } from "./parse-n8MmaK1x.js";
|
|
3
|
+
// src/job.ts
|
|
4
|
+
const waitFor = async (provider, id, check, settings) => {
|
|
5
|
+
const interval = settings.interval ?? 2e3;
|
|
6
|
+
const deadline = Date.now() + (settings.timeout ?? 6e5);
|
|
7
|
+
const request = {
|
|
8
|
+
retries: settings.retries,
|
|
9
|
+
signal: settings.signal
|
|
10
|
+
};
|
|
11
|
+
for (;;) {
|
|
12
|
+
if (settings.signal?.aborted === true) throw stopped(settings.signal, `stopped waiting for ${provider} job ${id}; it may still be running`, {
|
|
13
|
+
jobId: id,
|
|
14
|
+
provider
|
|
15
|
+
});
|
|
16
|
+
const found = await check(id, request);
|
|
17
|
+
if (found !== void 0) return found;
|
|
18
|
+
if (Date.now() + interval > deadline) throw new FieldworkError("Timeout", `${provider} job ${id} did not finish in time; it may still be running`, {
|
|
19
|
+
jobId: id,
|
|
20
|
+
provider
|
|
21
|
+
});
|
|
22
|
+
await sleep(interval, settings.signal);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Runs an asynchronous job to its result: resumes the job named by `resume`
|
|
27
|
+
* or starts one, checks it until it finishes, then builds the result.
|
|
28
|
+
*
|
|
29
|
+
* Every error after the job has an id carries it as `jobId`, including a
|
|
30
|
+
* timeout or cancellation while waiting, so the caller can resume rather than
|
|
31
|
+
* start a second job.
|
|
32
|
+
*/
|
|
33
|
+
const runJob = async (provider, job, settings) => {
|
|
34
|
+
const id = settings.resume ?? await job.start();
|
|
35
|
+
try {
|
|
36
|
+
return await job.finish(await waitFor(provider, id, job.check, settings), id);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw withJob(error, id);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export { runJob as t };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { B as ParsedDocument, C as DocumentInput, a as RawClient, p as CallOptions, t as Adapter } from "./adapter-BgdfLBs-.js";
|
|
2
|
+
export interface LlamaParseConfig {
|
|
3
|
+
/** Defaults to the LLAMA_CLOUD_API_KEY environment variable. */
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/** Defaults to https://api.cloud.llamaindex.ai; the EU region is https://api.cloud.eu.llamaindex.ai. */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
9
|
+
retries?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* LlamaParse's tiers, cheapest first. `fast` reads the text layer only, with
|
|
13
|
+
* no AI model; `agentic` reads tables, scans and multi-column layouts.
|
|
14
|
+
*/
|
|
15
|
+
export type LlamaParseTier = "fast" | "cost_effective" | "agentic" | "agentic_plus";
|
|
16
|
+
export interface LlamaParseParseOptions extends CallOptions {
|
|
17
|
+
/** Defaults to "agentic". */
|
|
18
|
+
tier?: LlamaParseTier;
|
|
19
|
+
/** The parser version: "latest" (the default) or a date such as "2026-09-28". */
|
|
20
|
+
version?: string;
|
|
21
|
+
/** Return layout blocks with positions. */
|
|
22
|
+
blocks?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Also return where each word and table cell sits in each block, from a
|
|
25
|
+
* second download. Returns blocks too. LlamaParse does not charge for it.
|
|
26
|
+
*/
|
|
27
|
+
wordBoxes?: boolean;
|
|
28
|
+
/** Leave out text a reader cannot see, such as white text on white. */
|
|
29
|
+
ignoreHiddenText?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Also return a picture of each page as `pages[].image`, an address that
|
|
32
|
+
* expires after about an hour. Collect fresh addresses with
|
|
33
|
+
* `parse({ resume: id, pageImages: true })`; a parse started without
|
|
34
|
+
* `pageImages` saved no pictures, so resuming it with them throws.
|
|
35
|
+
*/
|
|
36
|
+
pageImages?: boolean;
|
|
37
|
+
/** The id of a job from an error's `jobId`; waits for it instead of starting another. */
|
|
38
|
+
resume?: string;
|
|
39
|
+
/** Milliseconds between checks. Defaults to 2000. */
|
|
40
|
+
interval?: number;
|
|
41
|
+
}
|
|
42
|
+
/** Options for collecting an earlier parse by its id, without the file. */
|
|
43
|
+
export type LlamaParseResumeOptions = Omit<LlamaParseParseOptions, "resume"> & {
|
|
44
|
+
resume: string;
|
|
45
|
+
};
|
|
46
|
+
export interface LlamaParseOperations {
|
|
47
|
+
parse: {
|
|
48
|
+
(document: DocumentInput, options?: LlamaParseParseOptions): Promise<ParsedDocument>;
|
|
49
|
+
/** Collects an earlier parse by its id, without sending the file again. */
|
|
50
|
+
(options: LlamaParseResumeOptions): Promise<ParsedDocument>;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* An adapter for LlamaParse: parse. Pass it to `new Fieldwork({ adapter })`.
|
|
55
|
+
*
|
|
56
|
+
* Throws `Unauthorized` when neither `apiKey` nor `LLAMA_CLOUD_API_KEY` is set.
|
|
57
|
+
*/
|
|
58
|
+
export declare const llamaparse: (config?: LlamaParseConfig) => Adapter<"llamaparse", LlamaParseOperations, RawClient>;
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
+
import { a as refuseLarger, n as documentPart, r as enclosing } from "./document-Dpj01mGx.js";
|
|
3
|
+
import { t as FieldworkError } from "./errors-Yvk42oZH.js";
|
|
4
|
+
import { B as requireJobId, 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-n8MmaK1x.js";
|
|
5
|
+
import { t as runJob } from "./job-BGTwH3F-.js";
|
|
6
|
+
import * as z from "zod/mini";
|
|
7
|
+
// src/adapters/llamaparse/response.ts
|
|
8
|
+
/** A box in page points, from the page's top-left corner. */
|
|
9
|
+
const PointBox = looseObject({
|
|
10
|
+
h: optionalNumber,
|
|
11
|
+
w: optionalNumber,
|
|
12
|
+
x: optionalNumber,
|
|
13
|
+
y: optionalNumber
|
|
14
|
+
});
|
|
15
|
+
/** A word: `span` is `[start, end)` in UTF-8 bytes of its item's text. */
|
|
16
|
+
const Word = looseObject({
|
|
17
|
+
bbox: PointBox,
|
|
18
|
+
span: arrayOf(z.number())
|
|
19
|
+
});
|
|
20
|
+
const Line = looseObject({ words: arrayOf(Word) });
|
|
21
|
+
const Cell = looseObject({
|
|
22
|
+
bbox: arrayOf(PointBox),
|
|
23
|
+
lines: arrayOf(Line)
|
|
24
|
+
});
|
|
25
|
+
/** An item of a page. Its own `items` (a header's, a list's) are read one level at a time. */
|
|
26
|
+
const Item = looseObject({
|
|
27
|
+
bbox: arrayOf(PointBox),
|
|
28
|
+
grounding: looseObject({
|
|
29
|
+
lines: arrayOf(Line),
|
|
30
|
+
rows: arrayOf(arrayOf(Cell))
|
|
31
|
+
}),
|
|
32
|
+
items: arrayOf(anything),
|
|
33
|
+
md: optionalAnyString,
|
|
34
|
+
/** A table's cells as text; a number reads as its digits. */
|
|
35
|
+
rows: arrayOf(arrayOf(optionalScalar)),
|
|
36
|
+
type: optionalString,
|
|
37
|
+
value: optionalAnyString
|
|
38
|
+
});
|
|
39
|
+
/** A page of items: in `items.pages` of a job, and a row of the grounded items. */
|
|
40
|
+
const ItemsPage = looseObject({
|
|
41
|
+
items: arrayOf(Item),
|
|
42
|
+
page_height: optionalNumber,
|
|
43
|
+
page_number: optionalNumber,
|
|
44
|
+
page_width: optionalNumber
|
|
45
|
+
});
|
|
46
|
+
/** `POST /api/v2/parse/upload`. */
|
|
47
|
+
const JobStart = looseObject({ id: withOriginal(optionalId) });
|
|
48
|
+
/** `GET /api/v2/parse/{id}`, as it arrived beside the value read from it. */
|
|
49
|
+
const JobResult = withOriginal(looseObject({
|
|
50
|
+
images_content_metadata: looseObject({ images: arrayOf(looseObject({
|
|
51
|
+
filename: optionalString,
|
|
52
|
+
presigned_url: optionalString
|
|
53
|
+
})) }),
|
|
54
|
+
items: looseObject({ pages: arrayOf(ItemsPage) }),
|
|
55
|
+
job: looseObject({
|
|
56
|
+
error_message: optionalString,
|
|
57
|
+
status: optionalString,
|
|
58
|
+
usage: looseObject({ credits: optionalNumber })
|
|
59
|
+
}),
|
|
60
|
+
markdown: looseObject({ pages: arrayOf(looseObject({
|
|
61
|
+
markdown: optionalAnyString,
|
|
62
|
+
page_number: optionalNumber
|
|
63
|
+
})) }),
|
|
64
|
+
result_content_metadata: looseObject({ grounded_items: looseObject({ presigned_url: optionalString }) })
|
|
65
|
+
}));
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
const decoder = new TextDecoder();
|
|
68
|
+
const fraction = (box, size) => {
|
|
69
|
+
const { h, w, x, y } = box;
|
|
70
|
+
if (x === void 0 || y === void 0 || w === void 0 || h === void 0) return;
|
|
71
|
+
return {
|
|
72
|
+
height: h / size.height,
|
|
73
|
+
left: x / size.width,
|
|
74
|
+
top: y / size.height,
|
|
75
|
+
width: w / size.width
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
const around = (boxes, size) => enclosing(boxes.map((box) => fraction(box, size)).filter((box) => box !== void 0));
|
|
79
|
+
const wordsOf = (lines, text, size) => {
|
|
80
|
+
const bytes = encoder.encode(text);
|
|
81
|
+
return lines.flatMap((line) => line.words.flatMap((word) => {
|
|
82
|
+
const box = fraction(word.bbox, size);
|
|
83
|
+
const [start, end] = word.span;
|
|
84
|
+
if (box === void 0 || start === void 0 || end === void 0) return [];
|
|
85
|
+
return [{
|
|
86
|
+
box,
|
|
87
|
+
text: decoder.decode(bytes.slice(start, end))
|
|
88
|
+
}];
|
|
89
|
+
}));
|
|
90
|
+
};
|
|
91
|
+
const tableOf = (item, size) => {
|
|
92
|
+
const cells = [];
|
|
93
|
+
const words = [];
|
|
94
|
+
for (const [row, grounded] of item.grounding.rows.entries()) for (const [column, cell] of grounded.entries()) {
|
|
95
|
+
const text = item.rows[row]?.[column];
|
|
96
|
+
if (text !== void 0) {
|
|
97
|
+
cells.push(compact({
|
|
98
|
+
box: around(cell.bbox, size),
|
|
99
|
+
column,
|
|
100
|
+
row,
|
|
101
|
+
text
|
|
102
|
+
}));
|
|
103
|
+
words.push(...wordsOf(cell.lines, text, size));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
cells,
|
|
108
|
+
words
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
const blocksOfItem = (item, page, size) => {
|
|
112
|
+
if (item.items.length > 0) return item.items.flatMap((child) => blocksOfItem(z.parse(Item, child), page, size));
|
|
113
|
+
const text = item.md ?? item.value ?? "";
|
|
114
|
+
if (size === void 0) return [{
|
|
115
|
+
page,
|
|
116
|
+
text,
|
|
117
|
+
type: item.type ?? "unknown"
|
|
118
|
+
}];
|
|
119
|
+
const table = tableOf(item, size);
|
|
120
|
+
const words = [...wordsOf(item.grounding.lines, text, size), ...table.words];
|
|
121
|
+
return [compact({
|
|
122
|
+
box: around(item.bbox, size),
|
|
123
|
+
cells: table.cells.length > 0 ? table.cells : void 0,
|
|
124
|
+
page,
|
|
125
|
+
text,
|
|
126
|
+
type: item.type ?? "unknown",
|
|
127
|
+
words: words.length > 0 ? words : void 0
|
|
128
|
+
})];
|
|
129
|
+
};
|
|
130
|
+
/** The blocks of each page, its boxes turned from points into fractions of the page. */
|
|
131
|
+
const documentBlocks = (pages) => pages.flatMap((entry) => {
|
|
132
|
+
const page = entry.page_number;
|
|
133
|
+
if (page === void 0) return [];
|
|
134
|
+
const size = entry.page_width !== void 0 && entry.page_height !== void 0 && entry.page_width > 0 && entry.page_height > 0 ? {
|
|
135
|
+
height: entry.page_height,
|
|
136
|
+
width: entry.page_width
|
|
137
|
+
} : void 0;
|
|
138
|
+
return entry.items.flatMap((item) => blocksOfItem(item, page, size));
|
|
139
|
+
});
|
|
140
|
+
// src/adapters/llamaparse.ts
|
|
141
|
+
const provider = "llamaparse";
|
|
142
|
+
/** LlamaParse names each page's screenshot `page_<n>.jpg`. */
|
|
143
|
+
const PAGE_IMAGE = /^page_(?<page>\d+)\.jpg$/u;
|
|
144
|
+
/** The largest file LlamaParse takes, in megabytes. */
|
|
145
|
+
const MAX_PARSE_MB = 512;
|
|
146
|
+
const configuration = (o) => compact({
|
|
147
|
+
output_options: o.wordBoxes === true || o.pageImages === true ? compact({
|
|
148
|
+
granular_bboxes: o.wordBoxes === true ? [
|
|
149
|
+
"word",
|
|
150
|
+
"line",
|
|
151
|
+
"cell"
|
|
152
|
+
] : void 0,
|
|
153
|
+
images_to_save: o.pageImages === true ? ["screenshot"] : void 0
|
|
154
|
+
}) : void 0,
|
|
155
|
+
processing_options: o.ignoreHiddenText === true ? { ignore: { ignore_hidden_text: true } } : void 0,
|
|
156
|
+
tier: o.tier ?? "agentic",
|
|
157
|
+
version: o.version ?? "latest"
|
|
158
|
+
});
|
|
159
|
+
const startParse = async (raw, document, o, call) => {
|
|
160
|
+
if (document === void 0) throw new FieldworkError("InvalidRequest", "parse needs a document, or resume with the id of an earlier parse", { provider });
|
|
161
|
+
refuseLarger(provider, document, MAX_PARSE_MB);
|
|
162
|
+
const form = new FormData();
|
|
163
|
+
form.set("configuration", JSON.stringify(configuration(o)));
|
|
164
|
+
form.set("file", documentPart(document), document.name);
|
|
165
|
+
const created = z.parse(JobStart, await raw.request("POST", "/api/v2/parse/upload", {
|
|
166
|
+
creates: true,
|
|
167
|
+
...call,
|
|
168
|
+
form
|
|
169
|
+
}));
|
|
170
|
+
return requireJobId(provider, created.id);
|
|
171
|
+
};
|
|
172
|
+
const finished = (found) => {
|
|
173
|
+
const { error_message: message, status } = found.value.job;
|
|
174
|
+
if (status === "FAILED" || status === "CANCELLED") throw new FieldworkError("JobFailed", `llamaparse job ${status.toLowerCase()}${message === void 0 ? "" : `: ${message}`}`, {
|
|
175
|
+
cause: found.original,
|
|
176
|
+
provider
|
|
177
|
+
});
|
|
178
|
+
return status === "COMPLETED" ? found : void 0;
|
|
179
|
+
};
|
|
180
|
+
/** The grounded items: one JSON line per page, each page's items with word and cell boxes. */
|
|
181
|
+
const groundedPages = async (found, options) => {
|
|
182
|
+
const url = found.value.result_content_metadata.grounded_items.presigned_url;
|
|
183
|
+
if (url === void 0) throw new FieldworkError("ProviderError", "llamaparse finished without the word boxes it was asked for", {
|
|
184
|
+
cause: found.original,
|
|
185
|
+
provider
|
|
186
|
+
});
|
|
187
|
+
return (await readSigned(provider, url, options)).split("\n").filter((line) => line.trim() !== "").map((line) => parseJson(provider, line));
|
|
188
|
+
};
|
|
189
|
+
/** Each page's picture, keyed by page number, from the job's saved screenshots. */
|
|
190
|
+
const pageImageAddresses = (found, id) => {
|
|
191
|
+
const addresses = /* @__PURE__ */ new Map();
|
|
192
|
+
for (const image of found.value.images_content_metadata.images) {
|
|
193
|
+
const page = PAGE_IMAGE.exec(image.filename ?? "")?.groups?.page;
|
|
194
|
+
if (page !== void 0 && image.presigned_url !== void 0) addresses.set(Number(page), image.presigned_url);
|
|
195
|
+
}
|
|
196
|
+
if (addresses.size === 0) throw new FieldworkError("ProviderError", `llamaparse job ${id} has no page pictures; only a parse started with pageImages saves them`, {
|
|
197
|
+
cause: found.original,
|
|
198
|
+
provider
|
|
199
|
+
});
|
|
200
|
+
return addresses;
|
|
201
|
+
};
|
|
202
|
+
/** The finished job as a document, with word boxes and page pictures when they were asked for. */
|
|
203
|
+
const parsedDocument = async (found, id, o, read) => {
|
|
204
|
+
if (found.value.markdown.pages.length === 0) throw new FieldworkError("ProviderError", `llamaparse finished job ${id} without any pages of Markdown`, {
|
|
205
|
+
cause: found.original,
|
|
206
|
+
provider
|
|
207
|
+
});
|
|
208
|
+
const grounded = o.wordBoxes === true ? await groundedPages(found, read) : void 0;
|
|
209
|
+
const images = o.pageImages === true ? pageImageAddresses(found, id) : void 0;
|
|
210
|
+
const pages = found.value.markdown.pages.flatMap((page) => {
|
|
211
|
+
if (page.page_number === void 0) return [];
|
|
212
|
+
const image = images?.get(page.page_number);
|
|
213
|
+
if (images !== void 0 && image === void 0) throw new FieldworkError("ProviderError", `llamaparse job ${id} has no picture of page ${page.page_number}`, {
|
|
214
|
+
cause: found.original,
|
|
215
|
+
provider
|
|
216
|
+
});
|
|
217
|
+
return [compact({
|
|
218
|
+
image,
|
|
219
|
+
markdown: page.markdown ?? "",
|
|
220
|
+
pageNumber: page.page_number
|
|
221
|
+
})];
|
|
222
|
+
});
|
|
223
|
+
let laidOut;
|
|
224
|
+
if (grounded !== void 0) laidOut = documentBlocks(grounded.map((row) => z.parse(ItemsPage, row)));
|
|
225
|
+
else if (o.blocks === true) laidOut = documentBlocks(found.value.items.pages);
|
|
226
|
+
return compact({
|
|
227
|
+
blocks: laidOut,
|
|
228
|
+
id,
|
|
229
|
+
markdown: pages.map((page) => page.markdown).join("\n\n"),
|
|
230
|
+
meta: meta({ credits: found.value.job.usage.credits }),
|
|
231
|
+
pages,
|
|
232
|
+
raw: grounded === void 0 ? found.original : {
|
|
233
|
+
groundedItems: grounded,
|
|
234
|
+
job: found.original
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
const parse = async (raw, document, options, fetchImpl) => {
|
|
239
|
+
const { call, rest: o } = callSettings(options);
|
|
240
|
+
const expand = [
|
|
241
|
+
"markdown",
|
|
242
|
+
"usage",
|
|
243
|
+
...o.blocks === true || o.wordBoxes === true ? ["items"] : [],
|
|
244
|
+
...o.pageImages === true ? ["images_content_metadata"] : []
|
|
245
|
+
].join(",");
|
|
246
|
+
return await runJob(provider, {
|
|
247
|
+
check: async (id, request) => finished(z.parse(JobResult, await raw.request("GET", `/api/v2/parse/${segment(id)}`, {
|
|
248
|
+
...request,
|
|
249
|
+
query: { expand }
|
|
250
|
+
}))),
|
|
251
|
+
finish: async (found, id) => await parsedDocument(found, id, o, {
|
|
252
|
+
fetch: fetchImpl,
|
|
253
|
+
signal: call.signal
|
|
254
|
+
}),
|
|
255
|
+
start: async () => await startParse(raw, document, o, call)
|
|
256
|
+
}, {
|
|
257
|
+
...call,
|
|
258
|
+
interval: o.interval,
|
|
259
|
+
resume: o.resume
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* An adapter for LlamaParse: parse. Pass it to `new Fieldwork({ adapter })`.
|
|
264
|
+
*
|
|
265
|
+
* Throws `Unauthorized` when neither `apiKey` nor `LLAMA_CLOUD_API_KEY` is set.
|
|
266
|
+
*/
|
|
267
|
+
const llamaparse = (config = {}) => {
|
|
268
|
+
const raw = createRawClient({
|
|
269
|
+
baseUrl: config.baseUrl ?? "https://api.cloud.llamaindex.ai",
|
|
270
|
+
fetch: config.fetch,
|
|
271
|
+
headers: { authorization: `Bearer ${requireKey(provider, config.apiKey, "LLAMA_CLOUD_API_KEY")}` },
|
|
272
|
+
provider,
|
|
273
|
+
retries: config.retries
|
|
274
|
+
});
|
|
275
|
+
return defineAdapter({
|
|
276
|
+
name: provider,
|
|
277
|
+
operations: { async parse(first, options) {
|
|
278
|
+
return "resume" in first ? await parse(raw, void 0, first, config.fetch) : await parse(raw, first, options, config.fetch);
|
|
279
|
+
} },
|
|
280
|
+
raw
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
export { llamaparse };
|
package/dist/parallel.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
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-BgdfLBs-.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-Cupy7-7q.js";
|
|
3
3
|
import "./util-DhYr1OS3.js";
|
|
4
4
|
interface ParallelConfig {
|
|
5
5
|
/** Defaults to the PARALLEL_API_KEY environment variable. */
|
|
@@ -231,5 +231,10 @@ interface ParallelOperations {
|
|
|
231
231
|
events: (id: string, options?: ParallelMonitorEventsOptions) => Promise<MonitorEvents>;
|
|
232
232
|
};
|
|
233
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* An adapter for Parallel: search, images, read, readMany, extract, answer, research, discover, decide and monitor. Pass it to `new Fieldwork({ adapter })`.
|
|
236
|
+
*
|
|
237
|
+
* Throws `Unauthorized` when neither `apiKey` nor `PARALLEL_API_KEY` is set.
|
|
238
|
+
*/
|
|
234
239
|
export declare const parallel: (config?: ParallelConfig) => Adapter<"parallel", ParallelOperations, RawClient>;
|
|
235
240
|
export type { ParallelAnswerOptions, ParallelConfig, ParallelDecideOptions, ParallelDiscoverOptions, ParallelDiscovery, ParallelExtractOptions, ParallelImagesOptions, ParallelMetadata, ParallelMonitorCreateOptions, ParallelMonitorEventsOptions, ParallelOperations, ParallelProcessor, ParallelReadManyOptions, ParallelReadOptions, ParallelResearchOptions, ParallelSearchOptions, ParallelSources, ParallelWebhookEvent };
|