@anyformat/sdk 0.2.3 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +225 -35
- package/dist/index.cjs +511 -169
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1372 -328
- package/dist/index.d.ts +1372 -328
- package/dist/index.js +502 -162
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -23,34 +23,36 @@ __export(index_exports, {
|
|
|
23
23
|
APIError: () => APIError,
|
|
24
24
|
Anyformat: () => Anyformat,
|
|
25
25
|
AnyformatError: () => AnyformatError,
|
|
26
|
-
BadRequest: () => BadRequest,
|
|
27
26
|
BadRequestError: () => BadRequestError,
|
|
28
|
-
|
|
27
|
+
DatasetUpload: () => DatasetUpload,
|
|
28
|
+
ExtractionCancelledError: () => ExtractionCancelledError,
|
|
29
|
+
ExtractionFailedError: () => ExtractionFailedError,
|
|
29
30
|
ForbiddenError: () => ForbiddenError,
|
|
30
|
-
|
|
31
|
+
InsufficientScopeError: () => InsufficientScopeError,
|
|
31
32
|
NotFoundError: () => NotFoundError,
|
|
32
33
|
ParseView: () => ParseView,
|
|
33
|
-
|
|
34
|
+
PaymentRequiredError: () => PaymentRequiredError,
|
|
34
35
|
RateLimitedError: () => RateLimitedError,
|
|
35
36
|
Result: () => Result,
|
|
36
37
|
Run: () => Run,
|
|
37
|
-
|
|
38
|
+
RunFailedError: () => RunFailedError,
|
|
38
39
|
SDKTimeoutError: () => SDKTimeoutError,
|
|
39
40
|
Schema: () => Schema,
|
|
40
41
|
ServerError: () => ServerError,
|
|
41
|
-
|
|
42
|
+
TermsAcceptanceRequiredError: () => TermsAcceptanceRequiredError,
|
|
42
43
|
UnauthorizedError: () => UnauthorizedError,
|
|
43
44
|
Upload: () => Upload,
|
|
44
45
|
Workflow: () => Workflow,
|
|
45
46
|
WorkflowBuilder: () => WorkflowBuilder,
|
|
46
|
-
WorkflowBuilderError: () => WorkflowBuilderError
|
|
47
|
+
WorkflowBuilderError: () => WorkflowBuilderError,
|
|
48
|
+
WorkflowNameConflictError: () => WorkflowNameConflictError
|
|
47
49
|
});
|
|
48
50
|
module.exports = __toCommonJS(index_exports);
|
|
49
51
|
|
|
50
52
|
// package.json
|
|
51
53
|
var package_default = {
|
|
52
54
|
name: "@anyformat/sdk",
|
|
53
|
-
version: "0.2
|
|
55
|
+
version: "1.0.0-rc.2",
|
|
54
56
|
description: "TypeScript SDK for the AnyFormat API",
|
|
55
57
|
license: "MIT",
|
|
56
58
|
author: "Anyformat <info@anyformat.ai>",
|
|
@@ -72,10 +74,10 @@ var package_default = {
|
|
|
72
74
|
node: ">=18"
|
|
73
75
|
},
|
|
74
76
|
scripts: {
|
|
75
|
-
"gen:spec": 'ROOT=$(unset GIT_DIR && git rev-parse --show-toplevel) && cd "$ROOT/anyformat/services/api" && uv run python "$ROOT/anyformat/libs/public/sdk-js/scripts/dump_openapi.py" "$ROOT/anyformat/libs/public/sdk-js/openapi/v2.json"',
|
|
77
|
+
"gen:spec": 'ROOT=$(unset GIT_DIR && git rev-parse --show-toplevel) && cd "$ROOT/anyformat/services/api" && uv run python "$ROOT/anyformat/libs/public/sdk-js/scripts/dump_openapi.py" "$ROOT/anyformat/libs/public/sdk-js/openapi/v2.json" --surface v2 && uv run python "$ROOT/anyformat/libs/public/sdk-js/scripts/dump_openapi.py" "$ROOT/anyformat/libs/public/sdk-js/openapi/v3.json" --surface v3',
|
|
76
78
|
"gen:client": "openapi-ts",
|
|
77
79
|
gen: "bun run gen:spec && bun run gen:client",
|
|
78
|
-
"gen:check": "bun run gen && git diff --exit-code -- openapi
|
|
80
|
+
"gen:check": "bun run gen && git diff --exit-code -- openapi src/sdk",
|
|
79
81
|
build: "tsup",
|
|
80
82
|
typecheck: "tsc --noEmit",
|
|
81
83
|
test: "vitest run",
|
|
@@ -133,8 +135,8 @@ var Result = class {
|
|
|
133
135
|
}
|
|
134
136
|
raw;
|
|
135
137
|
_parse;
|
|
136
|
-
get
|
|
137
|
-
return this.raw.
|
|
138
|
+
get documentPacketId() {
|
|
139
|
+
return this.raw.document_packet_id;
|
|
138
140
|
}
|
|
139
141
|
get extractions() {
|
|
140
142
|
return this.raw.extractions ?? [];
|
|
@@ -151,16 +153,21 @@ var Result = class {
|
|
|
151
153
|
return this._parse;
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
154
|
-
* Convenience accessor for a top-level
|
|
155
|
-
* extraction
|
|
156
|
-
*
|
|
156
|
+
* Convenience accessor for a top-level field on the untagged (linear)
|
|
157
|
+
* extraction. Returns the scalar `ExtractedField` for a scalar field, or
|
|
158
|
+
* the array of row records (`ExtractedRows`) for a list/object field such
|
|
159
|
+
* as `line_items`. `undefined` only when the field is absent.
|
|
157
160
|
*
|
|
158
|
-
* Parameterise with the declared schema type to type `.value`:
|
|
161
|
+
* Parameterise with the declared schema type to type a scalar's `.value`:
|
|
159
162
|
* `result.field<number>("total").value` is `number | null`.
|
|
160
163
|
*/
|
|
161
164
|
field(name) {
|
|
162
|
-
const
|
|
163
|
-
|
|
165
|
+
const untagged = this.extractions.find(
|
|
166
|
+
(e) => e.split_name == null && e.partition == null
|
|
167
|
+
);
|
|
168
|
+
const value = untagged?.fields?.[name];
|
|
169
|
+
if (value === void 0) return void 0;
|
|
170
|
+
if (Array.isArray(value)) return value;
|
|
164
171
|
return value;
|
|
165
172
|
}
|
|
166
173
|
};
|
|
@@ -174,45 +181,116 @@ var APIError = class extends AnyformatError {
|
|
|
174
181
|
this.status = status;
|
|
175
182
|
this.body = body;
|
|
176
183
|
this.name = new.target.name;
|
|
184
|
+
if (body && typeof body === "object") {
|
|
185
|
+
const envelope = body;
|
|
186
|
+
if (typeof envelope.error_code === "string") this.errorCode = envelope.error_code;
|
|
187
|
+
if (envelope.detail !== void 0) this.detail = envelope.detail;
|
|
188
|
+
if (typeof envelope.retryable === "boolean") this.retryable = envelope.retryable;
|
|
189
|
+
if (typeof envelope.request_id === "string") this.requestId = envelope.request_id;
|
|
190
|
+
if (envelope.params && typeof envelope.params === "object") {
|
|
191
|
+
this.params = envelope.params;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
177
194
|
}
|
|
178
195
|
status;
|
|
179
196
|
body;
|
|
197
|
+
/** Stable identifier for programmatic handling, e.g. `PAYMENT_REQUIRED`. */
|
|
198
|
+
errorCode;
|
|
199
|
+
/** Developer-facing explanation; not surfaced in `message` (ADR platform/0053 §2). Often a `ValidationItem[]`. */
|
|
200
|
+
detail;
|
|
201
|
+
/** Named substitutions the `error` summary refers to, e.g. `{ required_scope }`. */
|
|
202
|
+
params;
|
|
203
|
+
/** Whether the API says retrying the same request can succeed. */
|
|
204
|
+
retryable;
|
|
205
|
+
/** Correlation id to quote in support requests. */
|
|
206
|
+
requestId;
|
|
180
207
|
};
|
|
181
208
|
var BadRequestError = class extends APIError {
|
|
182
209
|
};
|
|
183
210
|
var UnauthorizedError = class extends APIError {
|
|
184
211
|
};
|
|
212
|
+
var PaymentRequiredError = class extends APIError {
|
|
213
|
+
};
|
|
185
214
|
var ForbiddenError = class extends APIError {
|
|
186
215
|
};
|
|
187
216
|
var NotFoundError = class extends APIError {
|
|
188
217
|
};
|
|
189
218
|
var RateLimitedError = class extends APIError {
|
|
219
|
+
constructor(status, message, body, retryAfter) {
|
|
220
|
+
super(status, message, body);
|
|
221
|
+
this.retryAfter = retryAfter;
|
|
222
|
+
}
|
|
223
|
+
retryAfter;
|
|
190
224
|
};
|
|
191
225
|
var ServerError = class extends APIError {
|
|
192
226
|
};
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
227
|
+
function parseRetryAfter(raw) {
|
|
228
|
+
if (raw == null || raw.trim() === "") return void 0;
|
|
229
|
+
const seconds = Number(raw);
|
|
230
|
+
if (!Number.isNaN(seconds)) return seconds;
|
|
231
|
+
const when = Date.parse(raw);
|
|
232
|
+
if (Number.isNaN(when)) return void 0;
|
|
233
|
+
return Math.max((when - Date.now()) / 1e3, 0);
|
|
234
|
+
}
|
|
235
|
+
var InsufficientScopeError = class extends ForbiddenError {
|
|
236
|
+
};
|
|
237
|
+
var TermsAcceptanceRequiredError = class extends ForbiddenError {
|
|
238
|
+
};
|
|
239
|
+
var WorkflowNameConflictError = class extends APIError {
|
|
240
|
+
};
|
|
198
241
|
var SDKTimeoutError = class extends AnyformatError {
|
|
199
242
|
};
|
|
200
|
-
var
|
|
243
|
+
var RunFailedError = class extends AnyformatError {
|
|
244
|
+
constructor(runId, status) {
|
|
245
|
+
super(`Run ${runId} ended with status "${status}"`);
|
|
246
|
+
this.runId = runId;
|
|
247
|
+
this.status = status;
|
|
248
|
+
this.name = new.target.name;
|
|
249
|
+
}
|
|
250
|
+
runId;
|
|
251
|
+
status;
|
|
252
|
+
};
|
|
253
|
+
var ExtractionFailedError = class extends RunFailedError {
|
|
254
|
+
constructor(runId) {
|
|
255
|
+
super(runId, "error");
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
var ExtractionCancelledError = class extends RunFailedError {
|
|
259
|
+
constructor(runId) {
|
|
260
|
+
super(runId, "cancelled");
|
|
261
|
+
}
|
|
262
|
+
};
|
|
201
263
|
var WorkflowBuilderError = class extends AnyformatError {
|
|
202
264
|
};
|
|
203
|
-
function
|
|
204
|
-
const
|
|
265
|
+
function messageFrom(status, body) {
|
|
266
|
+
const fallback = `AnyFormat API error (${status})`;
|
|
267
|
+
if (!body || typeof body !== "object") return fallback;
|
|
268
|
+
const envelope = body;
|
|
269
|
+
return typeof envelope.error === "string" && envelope.error ? envelope.error : fallback;
|
|
270
|
+
}
|
|
271
|
+
var BY_ERROR_CODE = {
|
|
272
|
+
insufficient_api_key_scope: InsufficientScopeError,
|
|
273
|
+
terms_acceptance_required: TermsAcceptanceRequiredError,
|
|
274
|
+
WORKFLOW_NAME_CONFLICT: WorkflowNameConflictError
|
|
275
|
+
};
|
|
276
|
+
function toApiError(status, body, headers) {
|
|
277
|
+
const message = messageFrom(status, body);
|
|
278
|
+
const errorCode = body && typeof body === "object" && typeof body.error_code === "string" ? body.error_code : void 0;
|
|
279
|
+
const ByCode = errorCode ? BY_ERROR_CODE[errorCode] : void 0;
|
|
280
|
+
if (ByCode) return new ByCode(status, message, body);
|
|
205
281
|
switch (status) {
|
|
206
282
|
case 400:
|
|
207
283
|
return new BadRequestError(status, message, body);
|
|
208
284
|
case 401:
|
|
209
285
|
return new UnauthorizedError(status, message, body);
|
|
286
|
+
case 402:
|
|
287
|
+
return new PaymentRequiredError(status, message, body);
|
|
210
288
|
case 403:
|
|
211
289
|
return new ForbiddenError(status, message, body);
|
|
212
290
|
case 404:
|
|
213
291
|
return new NotFoundError(status, message, body);
|
|
214
292
|
case 429:
|
|
215
|
-
return new RateLimitedError(status, message, body);
|
|
293
|
+
return new RateLimitedError(status, message, body, parseRetryAfter(headers?.get("Retry-After")));
|
|
216
294
|
default:
|
|
217
295
|
return status >= 500 ? new ServerError(status, message, body) : new APIError(status, message, body);
|
|
218
296
|
}
|
|
@@ -272,7 +350,7 @@ function createSseClient({
|
|
|
272
350
|
...options
|
|
273
351
|
}) {
|
|
274
352
|
let lastEventId;
|
|
275
|
-
const
|
|
353
|
+
const sleep2 = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
276
354
|
const createStream = async function* () {
|
|
277
355
|
let retryDelay = sseDefaultRetryDelay ?? 3e3;
|
|
278
356
|
let attempt = 0;
|
|
@@ -376,7 +454,7 @@ function createSseClient({
|
|
|
376
454
|
break;
|
|
377
455
|
}
|
|
378
456
|
const backoff = Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 3e4);
|
|
379
|
-
await
|
|
457
|
+
await sleep2(backoff);
|
|
380
458
|
}
|
|
381
459
|
}
|
|
382
460
|
};
|
|
@@ -1032,73 +1110,112 @@ var createClient = (config = {}) => {
|
|
|
1032
1110
|
var client = createClient(createConfig({ baseUrl: "https://api.anyformat.dev" }));
|
|
1033
1111
|
|
|
1034
1112
|
// src/sdk/sdk.gen.ts
|
|
1035
|
-
var
|
|
1113
|
+
var v3DeleteDocumentPacket = (options) => (options.client ?? client).delete({
|
|
1114
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1115
|
+
url: "/v3/document-packets/{document_packet_id}/",
|
|
1116
|
+
...options
|
|
1117
|
+
});
|
|
1118
|
+
var v3GetDocumentPacket = (options) => (options.client ?? client).get({
|
|
1119
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1120
|
+
url: "/v3/document-packets/{document_packet_id}/",
|
|
1121
|
+
...options
|
|
1122
|
+
});
|
|
1123
|
+
var v3RunDocumentPacket = (options) => (options.client ?? client).post({
|
|
1124
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1125
|
+
url: "/v3/document-packets/{document_packet_id}/run/",
|
|
1126
|
+
...options
|
|
1127
|
+
});
|
|
1128
|
+
var v3Parse = (options) => (options.client ?? client).post({
|
|
1036
1129
|
...formDataBodySerializer,
|
|
1037
1130
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1038
|
-
url: "/
|
|
1131
|
+
url: "/v3/parse/",
|
|
1039
1132
|
...options,
|
|
1040
1133
|
headers: {
|
|
1041
1134
|
"Content-Type": null,
|
|
1042
1135
|
...options.headers
|
|
1043
1136
|
}
|
|
1044
1137
|
});
|
|
1045
|
-
var
|
|
1138
|
+
var v3GetRun = (options) => (options.client ?? client).get({
|
|
1046
1139
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1047
|
-
url: "/
|
|
1140
|
+
url: "/v3/runs/{run_id}/",
|
|
1048
1141
|
...options
|
|
1049
1142
|
});
|
|
1050
|
-
var
|
|
1143
|
+
var v3ListWorkflows = (options) => (options?.client ?? client).get({
|
|
1051
1144
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1052
|
-
url: "/
|
|
1145
|
+
url: "/v3/workflows/",
|
|
1053
1146
|
...options
|
|
1054
1147
|
});
|
|
1055
|
-
var
|
|
1148
|
+
var v3CreateWorkflow = (options) => (options.client ?? client).post({
|
|
1056
1149
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1057
|
-
url: "/
|
|
1150
|
+
url: "/v3/workflows/",
|
|
1058
1151
|
...options,
|
|
1059
1152
|
headers: {
|
|
1060
1153
|
"Content-Type": "application/json",
|
|
1061
1154
|
...options.headers
|
|
1062
1155
|
}
|
|
1063
1156
|
});
|
|
1064
|
-
var
|
|
1157
|
+
var v3DeleteWorkflow = (options) => (options.client ?? client).delete({
|
|
1158
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1159
|
+
url: "/v3/workflows/{workflow_id}/",
|
|
1160
|
+
...options
|
|
1161
|
+
});
|
|
1162
|
+
var v3GetWorkflow = (options) => (options.client ?? client).get({
|
|
1065
1163
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1066
|
-
url: "/
|
|
1164
|
+
url: "/v3/workflows/{workflow_id}/",
|
|
1067
1165
|
...options
|
|
1068
1166
|
});
|
|
1069
|
-
var
|
|
1167
|
+
var v3UpdateWorkflow = (options) => (options.client ?? client).patch({
|
|
1070
1168
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1071
|
-
url: "/
|
|
1169
|
+
url: "/v3/workflows/{workflow_id}/",
|
|
1072
1170
|
...options,
|
|
1073
1171
|
headers: {
|
|
1074
1172
|
"Content-Type": "application/json",
|
|
1075
1173
|
...options.headers
|
|
1076
1174
|
}
|
|
1077
1175
|
});
|
|
1078
|
-
var
|
|
1176
|
+
var v3UploadToDataset = (options) => (options.client ?? client).post({
|
|
1177
|
+
...formDataBodySerializer,
|
|
1178
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1179
|
+
url: "/v3/workflows/{workflow_id}/dataset/upload/",
|
|
1180
|
+
...options,
|
|
1181
|
+
headers: {
|
|
1182
|
+
"Content-Type": null,
|
|
1183
|
+
...options.headers
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
var v3ListWorkflowDocumentPackets = (options) => (options.client ?? client).get({
|
|
1079
1187
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1080
|
-
url: "/
|
|
1188
|
+
url: "/v3/workflows/{workflow_id}/document-packets/",
|
|
1081
1189
|
...options
|
|
1082
1190
|
});
|
|
1083
|
-
var
|
|
1191
|
+
var v3ListWorkflowRuns = (options) => (options.client ?? client).get({
|
|
1084
1192
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1085
|
-
url: "/
|
|
1193
|
+
url: "/v3/workflows/{workflow_id}/runs/",
|
|
1086
1194
|
...options
|
|
1087
1195
|
});
|
|
1088
|
-
var
|
|
1196
|
+
var v3UploadToWorkflow = (options) => (options.client ?? client).post({
|
|
1089
1197
|
...formDataBodySerializer,
|
|
1090
1198
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1091
|
-
url: "/
|
|
1199
|
+
url: "/v3/workflows/{workflow_id}/upload/",
|
|
1092
1200
|
...options,
|
|
1093
1201
|
headers: {
|
|
1094
1202
|
"Content-Type": null,
|
|
1095
1203
|
...options.headers
|
|
1096
1204
|
}
|
|
1097
1205
|
});
|
|
1098
|
-
var
|
|
1206
|
+
var v3UploadFromUrl = (options) => (options.client ?? client).post({
|
|
1207
|
+
security: [{ scheme: "bearer", type: "http" }],
|
|
1208
|
+
url: "/v3/workflows/{workflow_id}/upload/from-url/",
|
|
1209
|
+
...options,
|
|
1210
|
+
headers: {
|
|
1211
|
+
"Content-Type": "application/json",
|
|
1212
|
+
...options.headers
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
var v3UploadAndRunWorkflow = (options) => (options.client ?? client).post({
|
|
1099
1216
|
...formDataBodySerializer,
|
|
1100
1217
|
security: [{ scheme: "bearer", type: "http" }],
|
|
1101
|
-
url: "/
|
|
1218
|
+
url: "/v3/workflows/{workflow_id}/upload/run/",
|
|
1102
1219
|
...options,
|
|
1103
1220
|
headers: {
|
|
1104
1221
|
"Content-Type": null,
|
|
@@ -1107,73 +1224,128 @@ var v2UploadFile = (options) => (options.client ?? client).post({
|
|
|
1107
1224
|
});
|
|
1108
1225
|
|
|
1109
1226
|
// src/run.ts
|
|
1227
|
+
function isTransientStatus(status) {
|
|
1228
|
+
return status === 429 || status >= 500;
|
|
1229
|
+
}
|
|
1230
|
+
var RETRY_BASE_MS = 1e3;
|
|
1231
|
+
var RETRY_MAX_MS = 3e4;
|
|
1232
|
+
function retryDelayMs(response, attempt) {
|
|
1233
|
+
const retryAfter = parseRetryAfter(response?.headers?.get("Retry-After"));
|
|
1234
|
+
if (retryAfter !== void 0) return Math.min(retryAfter * 1e3, RETRY_MAX_MS);
|
|
1235
|
+
const base = Math.min(RETRY_BASE_MS * 2 ** attempt, RETRY_MAX_MS);
|
|
1236
|
+
return base * (0.75 + Math.random() * 0.5);
|
|
1237
|
+
}
|
|
1238
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1110
1239
|
var Run = class {
|
|
1111
1240
|
/** @internal */
|
|
1112
|
-
constructor(client2, workflowId,
|
|
1241
|
+
constructor(client2, id, workflowId, documentPacketId, status = "queued") {
|
|
1113
1242
|
this.client = client2;
|
|
1114
|
-
this.workflowId = workflowId;
|
|
1115
1243
|
this.id = id;
|
|
1244
|
+
this.workflowId = workflowId;
|
|
1245
|
+
this.documentPacketId = documentPacketId;
|
|
1246
|
+
this.status = status;
|
|
1116
1247
|
}
|
|
1117
1248
|
client;
|
|
1118
|
-
workflowId;
|
|
1119
1249
|
id;
|
|
1250
|
+
workflowId;
|
|
1251
|
+
documentPacketId;
|
|
1252
|
+
/** Status at trigger time, refreshed on every `wait()` poll. */
|
|
1253
|
+
status;
|
|
1120
1254
|
async wait(opts = {}) {
|
|
1121
1255
|
const timeoutMs = opts.timeoutMs ?? 3e5;
|
|
1122
1256
|
const pollMs = opts.pollMs ?? 3e3;
|
|
1123
1257
|
const deadline = Date.now() + timeoutMs;
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1258
|
+
let transientFailures = 0;
|
|
1259
|
+
for (; ; ) {
|
|
1260
|
+
const res = await v3GetRun({ client: this.client, path: { run_id: this.id } });
|
|
1261
|
+
if (res.error) {
|
|
1262
|
+
const status = res.response?.status ?? 500;
|
|
1263
|
+
if (!isTransientStatus(status)) throw toApiError(status, res.error, res.response?.headers);
|
|
1264
|
+
const remainingMs = deadline - Date.now();
|
|
1265
|
+
if (remainingMs <= 0) {
|
|
1266
|
+
throw new SDKTimeoutError(`Extraction did not complete within ${timeoutMs}ms`);
|
|
1267
|
+
}
|
|
1268
|
+
await sleep(Math.min(retryDelayMs(res.response, transientFailures), remainingMs));
|
|
1269
|
+
transientFailures += 1;
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
transientFailures = 0;
|
|
1273
|
+
const detail = res.data;
|
|
1274
|
+
this.status = detail.status;
|
|
1275
|
+
if (detail.status === "processed") {
|
|
1276
|
+
if (!detail.results) {
|
|
1277
|
+
throw new AnyformatError(`Run ${this.id} is processed but the API returned no results`);
|
|
1278
|
+
}
|
|
1279
|
+
return new Result(detail.results);
|
|
1280
|
+
}
|
|
1281
|
+
if (detail.status === "error") throw new ExtractionFailedError(this.id);
|
|
1282
|
+
if (detail.status === "cancelled") throw new ExtractionCancelledError(this.id);
|
|
1283
|
+
if (Date.now() >= deadline) {
|
|
1284
|
+
throw new SDKTimeoutError(`Extraction did not complete within ${timeoutMs}ms`);
|
|
1132
1285
|
}
|
|
1133
|
-
await
|
|
1286
|
+
await sleep(pollMs);
|
|
1134
1287
|
}
|
|
1135
|
-
throw new SDKTimeoutError(`Extraction did not complete within ${timeoutMs}ms`);
|
|
1136
1288
|
}
|
|
1137
1289
|
};
|
|
1138
1290
|
|
|
1139
1291
|
// src/upload.ts
|
|
1292
|
+
var DatasetUpload = class {
|
|
1293
|
+
/** @internal */
|
|
1294
|
+
constructor(workflowId, documentPacketId, files, groundTruthSaved) {
|
|
1295
|
+
this.workflowId = workflowId;
|
|
1296
|
+
this.documentPacketId = documentPacketId;
|
|
1297
|
+
this.files = files;
|
|
1298
|
+
this.groundTruthSaved = groundTruthSaved;
|
|
1299
|
+
}
|
|
1300
|
+
workflowId;
|
|
1301
|
+
documentPacketId;
|
|
1302
|
+
files;
|
|
1303
|
+
groundTruthSaved;
|
|
1304
|
+
};
|
|
1140
1305
|
var Upload = class {
|
|
1141
1306
|
/** @internal */
|
|
1142
|
-
constructor(client2, workflowId,
|
|
1307
|
+
constructor(client2, workflowId, documentPacketId, files) {
|
|
1143
1308
|
this.client = client2;
|
|
1144
1309
|
this.workflowId = workflowId;
|
|
1145
|
-
this.
|
|
1146
|
-
this.
|
|
1310
|
+
this.documentPacketId = documentPacketId;
|
|
1311
|
+
this.files = files;
|
|
1147
1312
|
}
|
|
1148
1313
|
client;
|
|
1149
1314
|
workflowId;
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
/** Trigger extraction on the staged
|
|
1153
|
-
async run() {
|
|
1154
|
-
|
|
1155
|
-
throw new WorkflowBuilderError("This upload has no file_id \u2014 cannot run it");
|
|
1156
|
-
}
|
|
1157
|
-
const res = await v2RunFile({
|
|
1315
|
+
documentPacketId;
|
|
1316
|
+
files;
|
|
1317
|
+
/** Trigger extraction on the staged packet and return a pending `Run`. */
|
|
1318
|
+
async run(opts = {}) {
|
|
1319
|
+
const res = await v3RunDocumentPacket({
|
|
1158
1320
|
client: this.client,
|
|
1159
|
-
path: {
|
|
1321
|
+
path: { document_packet_id: this.documentPacketId },
|
|
1322
|
+
...opts.idempotencyKey ? { headers: { "Idempotency-Key": opts.idempotencyKey } } : {}
|
|
1160
1323
|
});
|
|
1161
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1162
|
-
|
|
1324
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1325
|
+
const data = res.data;
|
|
1326
|
+
return new Run(this.client, data.run_id, data.workflow_id, data.document_packet_id, data.status);
|
|
1163
1327
|
}
|
|
1164
1328
|
};
|
|
1165
1329
|
|
|
1166
1330
|
// src/workflow.ts
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1331
|
+
var MAX_FILES_PER_PACKET = 100;
|
|
1332
|
+
function normalizeFiles(files, op) {
|
|
1333
|
+
const list = files == null ? [] : Array.isArray(files) ? files : [files];
|
|
1334
|
+
if (list.length === 0) {
|
|
1335
|
+
throw new WorkflowBuilderError(`${op}() requires at least one named file`);
|
|
1336
|
+
}
|
|
1337
|
+
if (list.length > MAX_FILES_PER_PACKET) {
|
|
1338
|
+
throw new WorkflowBuilderError(`${op}() accepts at most ${MAX_FILES_PER_PACKET} files per document packet`);
|
|
1339
|
+
}
|
|
1340
|
+
for (const file of list) {
|
|
1341
|
+
const filename = file.name;
|
|
1342
|
+
if (typeof filename !== "string" || filename.length === 0) {
|
|
1343
|
+
throw new WorkflowBuilderError(
|
|
1344
|
+
`${op}() needs a named file so the server can detect its type \u2014 pass a File, e.g. new File([bytes], "invoice.pdf"), not a bare Blob`
|
|
1345
|
+
);
|
|
1346
|
+
}
|
|
1176
1347
|
}
|
|
1348
|
+
return list;
|
|
1177
1349
|
}
|
|
1178
1350
|
var Workflow = class {
|
|
1179
1351
|
/** @internal */
|
|
@@ -1185,36 +1357,90 @@ var Workflow = class {
|
|
|
1185
1357
|
client;
|
|
1186
1358
|
id;
|
|
1187
1359
|
name;
|
|
1188
|
-
/**
|
|
1189
|
-
async run(
|
|
1190
|
-
|
|
1191
|
-
const submitted = await
|
|
1360
|
+
/** Upload 1..100 files as one document packet and run it. Returns a pending `Run`. */
|
|
1361
|
+
async run(files = null, opts = {}) {
|
|
1362
|
+
const list = normalizeFiles(files, "run");
|
|
1363
|
+
const submitted = await v3UploadAndRunWorkflow({
|
|
1192
1364
|
client: this.client,
|
|
1193
1365
|
path: { workflow_id: this.id },
|
|
1194
|
-
body: {
|
|
1366
|
+
body: { files: list, ...opts.onConflict ? { on_conflict: opts.onConflict } : {} },
|
|
1367
|
+
...opts.idempotencyKey ? { headers: { "Idempotency-Key": opts.idempotencyKey } } : {}
|
|
1195
1368
|
});
|
|
1196
|
-
if (submitted.error) throw toApiError(submitted.response?.status ?? 500, submitted.error);
|
|
1197
|
-
|
|
1369
|
+
if (submitted.error) throw toApiError(submitted.response?.status ?? 500, submitted.error, submitted.response?.headers);
|
|
1370
|
+
const data = submitted.data;
|
|
1371
|
+
return new Run(this.client, data.run_id, data.workflow_id, data.document_packet_id, data.status);
|
|
1198
1372
|
}
|
|
1199
1373
|
/**
|
|
1200
|
-
* Stage
|
|
1201
|
-
* Returns an `Upload`; call `.run()` on it to start extraction.
|
|
1374
|
+
* Stage 1..100 files as one document packet WITHOUT running it (no credits
|
|
1375
|
+
* spent). Returns an `Upload`; call `.run()` on it to start extraction.
|
|
1202
1376
|
*/
|
|
1203
|
-
async upload(
|
|
1204
|
-
|
|
1205
|
-
const staged = await
|
|
1377
|
+
async upload(files = null, opts = {}) {
|
|
1378
|
+
const list = normalizeFiles(files, "upload");
|
|
1379
|
+
const staged = await v3UploadToWorkflow({
|
|
1206
1380
|
client: this.client,
|
|
1207
1381
|
path: { workflow_id: this.id },
|
|
1208
|
-
body: {
|
|
1382
|
+
body: { files: list, ...opts.onConflict ? { on_conflict: opts.onConflict } : {} },
|
|
1383
|
+
...opts.idempotencyKey ? { headers: { "Idempotency-Key": opts.idempotencyKey } } : {}
|
|
1209
1384
|
});
|
|
1210
|
-
if (staged.error) throw toApiError(staged.response?.status ?? 500, staged.error);
|
|
1211
|
-
const
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1385
|
+
if (staged.error) throw toApiError(staged.response?.status ?? 500, staged.error, staged.response?.headers);
|
|
1386
|
+
const data = staged.data;
|
|
1387
|
+
return new Upload(this.client, data.workflow_id, data.document_packet_id, data.files);
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Upload 1..100 files as ONE document (+ optional ground truth) into the
|
|
1391
|
+
* workflow's dataset, as one document packet. Atomic: any rejected file or a
|
|
1392
|
+
* ground-truth failure fails the call and stores nothing. By default a
|
|
1393
|
+
* colliding filename fails with `409`; pass `onConflict: "rename"` to
|
|
1394
|
+
* auto-rename instead.
|
|
1395
|
+
*/
|
|
1396
|
+
async uploadToDataset(files = null, opts = {}) {
|
|
1397
|
+
const list = normalizeFiles(files, "uploadToDataset");
|
|
1398
|
+
const registered = await v3UploadToDataset({
|
|
1399
|
+
client: this.client,
|
|
1400
|
+
path: { workflow_id: this.id },
|
|
1401
|
+
body: {
|
|
1402
|
+
files: list,
|
|
1403
|
+
...opts.groundTruth != null ? { ground_truth: JSON.stringify(opts.groundTruth) } : {},
|
|
1404
|
+
...opts.onConflict ? { on_conflict: opts.onConflict } : {}
|
|
1405
|
+
},
|
|
1406
|
+
...opts.idempotencyKey ? { headers: { "Idempotency-Key": opts.idempotencyKey } } : {}
|
|
1407
|
+
});
|
|
1408
|
+
if (registered.error) throw toApiError(registered.response?.status ?? 500, registered.error, registered.response?.headers);
|
|
1409
|
+
const data = registered.data;
|
|
1410
|
+
return new DatasetUpload(this.id, data.document_packet_id, data.files, data.ground_truth_saved);
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Client-orchestrated bulk dataset upload: one atomic call per document,
|
|
1414
|
+
* in order. There is no batch endpoint — each document is bounded and
|
|
1415
|
+
* independently retryable. On failure the call rejects; documents already
|
|
1416
|
+
* uploaded stay registered.
|
|
1417
|
+
*/
|
|
1418
|
+
async uploadDocumentsToDataset(documents) {
|
|
1419
|
+
const uploaded = [];
|
|
1420
|
+
for (const doc of documents) {
|
|
1421
|
+
uploaded.push(
|
|
1422
|
+
await this.uploadToDataset(doc.files, {
|
|
1423
|
+
groundTruth: doc.groundTruth,
|
|
1424
|
+
idempotencyKey: doc.idempotencyKey,
|
|
1425
|
+
onConflict: doc.onConflict
|
|
1426
|
+
})
|
|
1215
1427
|
);
|
|
1216
1428
|
}
|
|
1217
|
-
return
|
|
1429
|
+
return uploaded;
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* Import 1..10 HTTPS URLs server-side into one document packet, atomically.
|
|
1433
|
+
* Returns an `Upload`; call `.run()` on it to start extraction.
|
|
1434
|
+
*/
|
|
1435
|
+
async uploadFromUrls(urls, opts = {}) {
|
|
1436
|
+
const staged = await v3UploadFromUrl({
|
|
1437
|
+
client: this.client,
|
|
1438
|
+
path: { workflow_id: this.id },
|
|
1439
|
+
body: { urls, ...opts.onConflict ? { on_conflict: opts.onConflict } : {} }
|
|
1440
|
+
});
|
|
1441
|
+
if (staged.error) throw toApiError(staged.response?.status ?? 500, staged.error, staged.response?.headers);
|
|
1442
|
+
const data = staged.data;
|
|
1443
|
+
return new Upload(this.client, data.workflow_id, data.document_packet_id, data.files);
|
|
1218
1444
|
}
|
|
1219
1445
|
};
|
|
1220
1446
|
|
|
@@ -1253,12 +1479,7 @@ var WorkflowBuilder = class {
|
|
|
1253
1479
|
figure_enhancement: o.figureEnhancement ?? false,
|
|
1254
1480
|
cache: o.cache ?? true,
|
|
1255
1481
|
...o.effort !== void 0 ? { effort: o.effort } : {},
|
|
1256
|
-
...o.
|
|
1257
|
-
...o.region !== void 0 ? { region: o.region } : {},
|
|
1258
|
-
...o.skipRoutingReview !== void 0 ? { skip_routing_review: o.skipRoutingReview } : {},
|
|
1259
|
-
...o.figures !== void 0 ? { figures: o.figures } : {},
|
|
1260
|
-
...o.textFormatting !== void 0 ? { text_formatting: o.textFormatting } : {},
|
|
1261
|
-
...o.routingEffort !== void 0 ? { routing_effort: o.routingEffort } : {}
|
|
1482
|
+
...o.scannedPages !== void 0 ? { scanned_pages: o.scannedPages } : {}
|
|
1262
1483
|
};
|
|
1263
1484
|
return this;
|
|
1264
1485
|
}
|
|
@@ -1392,16 +1613,16 @@ var WorkflowBuilder = class {
|
|
|
1392
1613
|
}
|
|
1393
1614
|
return { name: this.name, description: this.description, nodes, edges };
|
|
1394
1615
|
}
|
|
1395
|
-
/** Persist the workflow and return a handle you can `.run(
|
|
1616
|
+
/** Persist the workflow and return a handle you can `.run(files)` on. */
|
|
1396
1617
|
async create() {
|
|
1397
|
-
const res = await
|
|
1398
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1618
|
+
const res = await v3CreateWorkflow({ client: this.client, body: this.build() });
|
|
1619
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1399
1620
|
return new Workflow(this.client, res.data.id, res.data.name);
|
|
1400
1621
|
}
|
|
1401
|
-
/** Replace an existing workflow's
|
|
1622
|
+
/** Replace an existing workflow's typed graph and return a handle you can `.run(files)` on. */
|
|
1402
1623
|
async update(workflowId) {
|
|
1403
|
-
const res = await
|
|
1404
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1624
|
+
const res = await v3UpdateWorkflow({ client: this.client, path: { workflow_id: workflowId }, body: this.build() });
|
|
1625
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1405
1626
|
return new Workflow(this.client, res.data.id, res.data.name);
|
|
1406
1627
|
}
|
|
1407
1628
|
};
|
|
@@ -1429,75 +1650,194 @@ var Anyformat = class {
|
|
|
1429
1650
|
workflow(name, description) {
|
|
1430
1651
|
return new WorkflowBuilder(this._client, name, description);
|
|
1431
1652
|
}
|
|
1432
|
-
async listWorkflows(opts) {
|
|
1433
|
-
const res = await
|
|
1653
|
+
async listWorkflows(opts = {}) {
|
|
1654
|
+
const res = await v3ListWorkflows({
|
|
1655
|
+
client: this._client,
|
|
1656
|
+
query: { limit: opts.limit, cursor: opts.cursor }
|
|
1657
|
+
});
|
|
1658
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1659
|
+
return {
|
|
1660
|
+
items: res.data.items.map((r) => new Workflow(this._client, r.id, r.name)),
|
|
1661
|
+
nextCursor: res.data.next_cursor ?? null
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
/** Retrieve a workflow with its complete typed graph inline (round-trippable into `updateWorkflow`). */
|
|
1665
|
+
async getWorkflow(workflowId) {
|
|
1666
|
+
const res = await v3GetWorkflow({ client: this._client, path: { workflow_id: workflowId } });
|
|
1667
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1668
|
+
return res.data;
|
|
1669
|
+
}
|
|
1670
|
+
/**
|
|
1671
|
+
* Replace a workflow's typed graph (atomic). Accepts the shape `getWorkflow`
|
|
1672
|
+
* returns — echo each field's `persistent_id` unchanged (including across
|
|
1673
|
+
* renames) to keep the field's identity; omit it for new fields.
|
|
1674
|
+
*/
|
|
1675
|
+
async updateWorkflow(workflowId, definition) {
|
|
1676
|
+
const res = await v3UpdateWorkflow({
|
|
1434
1677
|
client: this._client,
|
|
1435
|
-
|
|
1678
|
+
path: { workflow_id: workflowId },
|
|
1679
|
+
// The PATCH body forbids unknown keys, so a fetched detail's read-only
|
|
1680
|
+
// fields (id, timestamps) must not be forwarded.
|
|
1681
|
+
body: {
|
|
1682
|
+
name: definition.name,
|
|
1683
|
+
description: definition.description,
|
|
1684
|
+
nodes: definition.nodes,
|
|
1685
|
+
edges: definition.edges
|
|
1686
|
+
}
|
|
1436
1687
|
});
|
|
1437
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1438
|
-
return res.data
|
|
1688
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1689
|
+
return res.data;
|
|
1439
1690
|
}
|
|
1440
1691
|
async deleteWorkflow(workflowId) {
|
|
1441
|
-
const res = await
|
|
1692
|
+
const res = await v3DeleteWorkflow({
|
|
1442
1693
|
client: this._client,
|
|
1443
1694
|
path: { workflow_id: workflowId }
|
|
1444
1695
|
});
|
|
1445
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1696
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1446
1697
|
return workflowId;
|
|
1447
1698
|
}
|
|
1448
|
-
/** Stage
|
|
1449
|
-
upload(workflowId,
|
|
1450
|
-
return new Workflow(this._client, workflowId, "").upload(
|
|
1451
|
-
}
|
|
1452
|
-
/**
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1699
|
+
/** Stage 1..100 files as one document packet without running it. Returns an `Upload`. */
|
|
1700
|
+
upload(workflowId, files, opts = {}) {
|
|
1701
|
+
return new Workflow(this._client, workflowId, "").upload(files, opts);
|
|
1702
|
+
}
|
|
1703
|
+
/** Upload 1..100 files (+ optional ground truth) as one dataset collection. Returns a `DatasetUpload`. */
|
|
1704
|
+
uploadToDataset(workflowId, files, opts = {}) {
|
|
1705
|
+
return new Workflow(this._client, workflowId, "").uploadToDataset(files, opts);
|
|
1706
|
+
}
|
|
1707
|
+
/** Bulk-upload documents into a workflow's dataset, one atomic call per document. */
|
|
1708
|
+
uploadDocumentsToDataset(workflowId, documents) {
|
|
1709
|
+
return new Workflow(this._client, workflowId, "").uploadDocumentsToDataset(documents);
|
|
1710
|
+
}
|
|
1711
|
+
async getDocumentPacket(documentPacketId) {
|
|
1712
|
+
const res = await v3GetDocumentPacket({
|
|
1713
|
+
client: this._client,
|
|
1714
|
+
path: { document_packet_id: documentPacketId }
|
|
1715
|
+
});
|
|
1716
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1717
|
+
return res.data;
|
|
1718
|
+
}
|
|
1719
|
+
/** Delete a document packet and all its files. Irreversible. */
|
|
1720
|
+
async deleteDocumentPacket(documentPacketId) {
|
|
1721
|
+
const res = await v3DeleteDocumentPacket({
|
|
1722
|
+
client: this._client,
|
|
1723
|
+
path: { document_packet_id: documentPacketId }
|
|
1724
|
+
});
|
|
1725
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1726
|
+
return documentPacketId;
|
|
1727
|
+
}
|
|
1728
|
+
/** Run (or re-run) a document packet on the latest version of its workflow. */
|
|
1729
|
+
async runDocumentPacket(documentPacketId, opts = {}) {
|
|
1730
|
+
const res = await v3RunDocumentPacket({
|
|
1731
|
+
client: this._client,
|
|
1732
|
+
path: { document_packet_id: documentPacketId },
|
|
1733
|
+
...opts.idempotencyKey ? { headers: { "Idempotency-Key": opts.idempotencyKey } } : {}
|
|
1734
|
+
});
|
|
1735
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1736
|
+
const data = res.data;
|
|
1737
|
+
return new Run(this._client, data.run_id, data.workflow_id, data.document_packet_id, data.status);
|
|
1738
|
+
}
|
|
1739
|
+
async listDocumentPackets(workflowId, opts = {}) {
|
|
1740
|
+
const res = await v3ListWorkflowDocumentPackets({
|
|
1741
|
+
client: this._client,
|
|
1742
|
+
path: { workflow_id: workflowId },
|
|
1743
|
+
query: { limit: opts.limit, cursor: opts.cursor }
|
|
1744
|
+
});
|
|
1745
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1746
|
+
return { items: res.data.items, nextCursor: res.data.next_cursor ?? null };
|
|
1747
|
+
}
|
|
1748
|
+
async listRuns(workflowId, opts = {}) {
|
|
1749
|
+
const res = await v3ListWorkflowRuns({
|
|
1750
|
+
client: this._client,
|
|
1751
|
+
path: { workflow_id: workflowId },
|
|
1752
|
+
query: { limit: opts.limit, cursor: opts.cursor }
|
|
1753
|
+
});
|
|
1754
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1755
|
+
return { items: res.data.items, nextCursor: res.data.next_cursor ?? null };
|
|
1756
|
+
}
|
|
1757
|
+
/**
|
|
1758
|
+
* Every workflow, following `nextCursor` across pages so no rows are silently
|
|
1759
|
+
* truncated. Lazy — each page is fetched on demand as the iterator advances.
|
|
1760
|
+
*/
|
|
1761
|
+
async *iterWorkflows(opts = {}) {
|
|
1762
|
+
let cursor;
|
|
1763
|
+
for (; ; ) {
|
|
1764
|
+
const page = await this.listWorkflows({ limit: opts.limit, cursor });
|
|
1765
|
+
yield* page.items;
|
|
1766
|
+
if (page.nextCursor === null) return;
|
|
1767
|
+
cursor = page.nextCursor;
|
|
1462
1768
|
}
|
|
1463
|
-
|
|
1769
|
+
}
|
|
1770
|
+
/** Every document packet of a workflow, following `nextCursor` across pages. Lazy. */
|
|
1771
|
+
async *iterDocumentPackets(workflowId, opts = {}) {
|
|
1772
|
+
let cursor;
|
|
1773
|
+
for (; ; ) {
|
|
1774
|
+
const page = await this.listDocumentPackets(workflowId, { limit: opts.limit, cursor });
|
|
1775
|
+
yield* page.items;
|
|
1776
|
+
if (page.nextCursor === null) return;
|
|
1777
|
+
cursor = page.nextCursor;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
/** Every run of a workflow, following `nextCursor` across pages. Lazy. */
|
|
1781
|
+
async *iterRuns(workflowId, opts = {}) {
|
|
1782
|
+
let cursor;
|
|
1783
|
+
for (; ; ) {
|
|
1784
|
+
const page = await this.listRuns(workflowId, { limit: opts.limit, cursor });
|
|
1785
|
+
yield* page.items;
|
|
1786
|
+
if (page.nextCursor === null) return;
|
|
1787
|
+
cursor = page.nextCursor;
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
/** Retrieve one run: lifecycle status plus results inline once `processed`. */
|
|
1791
|
+
async getRun(runId) {
|
|
1792
|
+
const res = await v3GetRun({ client: this._client, path: { run_id: runId } });
|
|
1793
|
+
if (res.error) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1794
|
+
return res.data;
|
|
1464
1795
|
}
|
|
1465
1796
|
/**
|
|
1466
|
-
* Parse a document with the platform's atomic
|
|
1467
|
-
* parse). Returns
|
|
1468
|
-
*
|
|
1797
|
+
* Parse a document with the platform's atomic parse operation (fast lite
|
|
1798
|
+
* parse). Returns the run id; poll `getParseMarkdown(runId)` for the parsed
|
|
1799
|
+
* markdown.
|
|
1469
1800
|
*/
|
|
1470
1801
|
async parse(file) {
|
|
1471
|
-
const res = await
|
|
1472
|
-
if (res.error) throw toApiError(res.response?.status ?? 500, res.error);
|
|
1473
|
-
return res.data.
|
|
1802
|
+
const res = await v3Parse({ client: this._client, body: { file } });
|
|
1803
|
+
if (res.error !== void 0) throw toApiError(res.response?.status ?? 500, res.error, res.response?.headers);
|
|
1804
|
+
return res.data.run_id;
|
|
1474
1805
|
}
|
|
1475
1806
|
/**
|
|
1476
|
-
* Poll a parse
|
|
1477
|
-
* `null` while it is still
|
|
1807
|
+
* Poll a parse run. Returns the parsed markdown once the run is `processed`,
|
|
1808
|
+
* or `null` while it is still queued or in progress.
|
|
1478
1809
|
*/
|
|
1479
|
-
async getParseMarkdown(
|
|
1480
|
-
const
|
|
1481
|
-
if (
|
|
1482
|
-
|
|
1810
|
+
async getParseMarkdown(runId) {
|
|
1811
|
+
const run = await this.getRun(runId);
|
|
1812
|
+
if (run.status === "processed") {
|
|
1813
|
+
const markdown = run.results?.parse?.markdown;
|
|
1814
|
+
if (markdown == null) throw new AnyformatError(`Run ${runId} is processed but the API returned no results`);
|
|
1815
|
+
return markdown;
|
|
1816
|
+
}
|
|
1817
|
+
if (run.status === "error") throw new ExtractionFailedError(runId);
|
|
1818
|
+
if (run.status === "cancelled") throw new ExtractionCancelledError(runId);
|
|
1819
|
+
return null;
|
|
1483
1820
|
}
|
|
1484
1821
|
};
|
|
1485
1822
|
|
|
1486
1823
|
// src/schema.ts
|
|
1487
|
-
function
|
|
1488
|
-
|
|
1489
|
-
|
|
1824
|
+
function withOpts(base, opts) {
|
|
1825
|
+
return {
|
|
1826
|
+
...base,
|
|
1827
|
+
...opts?.source && opts.source !== "extraction" ? { source: opts.source } : {},
|
|
1828
|
+
...opts?.persistentId !== void 0 ? { persistent_id: opts.persistentId } : {}
|
|
1829
|
+
};
|
|
1490
1830
|
}
|
|
1491
1831
|
var Schema = {
|
|
1492
|
-
string: (name, description, opts) =>
|
|
1493
|
-
integer: (name, description, opts) =>
|
|
1494
|
-
float: (name, description, opts) =>
|
|
1495
|
-
boolean: (name, description, opts) =>
|
|
1496
|
-
date: (name, description, opts) =>
|
|
1497
|
-
datetime: (name, description, opts) =>
|
|
1498
|
-
enum: (name, description, options, opts) =>
|
|
1499
|
-
multiSelect: (name, description, options, opts) =>
|
|
1500
|
-
object: (name, description, fields, opts) =>
|
|
1832
|
+
string: (name, description, opts) => withOpts({ data_type: "string", name, description }, opts),
|
|
1833
|
+
integer: (name, description, opts) => withOpts({ data_type: "integer", name, description }, opts),
|
|
1834
|
+
float: (name, description, opts) => withOpts({ data_type: "float", name, description }, opts),
|
|
1835
|
+
boolean: (name, description, opts) => withOpts({ data_type: "boolean", name, description }, opts),
|
|
1836
|
+
date: (name, description, opts) => withOpts({ data_type: "date", name, description }, opts),
|
|
1837
|
+
datetime: (name, description, opts) => withOpts({ data_type: "datetime", name, description }, opts),
|
|
1838
|
+
enum: (name, description, options, opts) => withOpts({ data_type: "enum", name, description, enum_options: options }, opts),
|
|
1839
|
+
multiSelect: (name, description, options, opts) => withOpts({ data_type: "multi_select", name, description, enum_options: options }, opts),
|
|
1840
|
+
object: (name, description, fields, opts) => withOpts({ data_type: "object", name, description, nested_fields: fields }, opts),
|
|
1501
1841
|
option: (name, description) => ({ name, description })
|
|
1502
1842
|
};
|
|
1503
1843
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1505,26 +1845,28 @@ var Schema = {
|
|
|
1505
1845
|
APIError,
|
|
1506
1846
|
Anyformat,
|
|
1507
1847
|
AnyformatError,
|
|
1508
|
-
BadRequest,
|
|
1509
1848
|
BadRequestError,
|
|
1510
|
-
|
|
1849
|
+
DatasetUpload,
|
|
1850
|
+
ExtractionCancelledError,
|
|
1851
|
+
ExtractionFailedError,
|
|
1511
1852
|
ForbiddenError,
|
|
1512
|
-
|
|
1853
|
+
InsufficientScopeError,
|
|
1513
1854
|
NotFoundError,
|
|
1514
1855
|
ParseView,
|
|
1515
|
-
|
|
1856
|
+
PaymentRequiredError,
|
|
1516
1857
|
RateLimitedError,
|
|
1517
1858
|
Result,
|
|
1518
1859
|
Run,
|
|
1519
|
-
|
|
1860
|
+
RunFailedError,
|
|
1520
1861
|
SDKTimeoutError,
|
|
1521
1862
|
Schema,
|
|
1522
1863
|
ServerError,
|
|
1523
|
-
|
|
1864
|
+
TermsAcceptanceRequiredError,
|
|
1524
1865
|
UnauthorizedError,
|
|
1525
1866
|
Upload,
|
|
1526
1867
|
Workflow,
|
|
1527
1868
|
WorkflowBuilder,
|
|
1528
|
-
WorkflowBuilderError
|
|
1869
|
+
WorkflowBuilderError,
|
|
1870
|
+
WorkflowNameConflictError
|
|
1529
1871
|
});
|
|
1530
1872
|
//# sourceMappingURL=index.cjs.map
|