@docmana/sdk 0.46.0 → 0.48.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -21
- package/README.md +55 -0
- package/dist/cli.mjs +466 -218
- package/dist/cli.mjs.map +1 -1
- package/dist/execution-events.d.ts +2 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +490 -234
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +486 -234
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +28 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -182,6 +182,365 @@ var FormulaAuthoring = class {
|
|
|
182
182
|
}
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
+
// src/sdk/errors.ts
|
|
186
|
+
var DocmanaError = class extends Error {
|
|
187
|
+
status;
|
|
188
|
+
requestId;
|
|
189
|
+
code;
|
|
190
|
+
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
191
|
+
apiCode;
|
|
192
|
+
reviewConflict;
|
|
193
|
+
constructor(message2, opts = {}) {
|
|
194
|
+
super(message2);
|
|
195
|
+
this.name = new.target.name;
|
|
196
|
+
this.status = opts.status;
|
|
197
|
+
this.requestId = opts.requestId;
|
|
198
|
+
this.code = opts.code ?? "docmana_error";
|
|
199
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var DocmanaAuthError = class extends DocmanaError {
|
|
203
|
+
constructor(message2, opts = {}) {
|
|
204
|
+
super(message2, { ...opts, code: "auth_error" });
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
var DocmanaPermissionError = class extends DocmanaError {
|
|
208
|
+
constructor(message2, opts = {}) {
|
|
209
|
+
super(message2, { ...opts, code: "permission_error" });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var DocmanaNotFoundError = class extends DocmanaError {
|
|
213
|
+
constructor(message2, opts = {}) {
|
|
214
|
+
super(message2, { ...opts, code: "not_found" });
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var DocmanaRequestError = class extends DocmanaError {
|
|
218
|
+
constructor(message2, opts = {}) {
|
|
219
|
+
super(message2, { ...opts, code: "bad_request" });
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
223
|
+
constructor(message2, opts = {}) {
|
|
224
|
+
super(message2, { ...opts, code: "payload_too_large" });
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var DocmanaExecutionError = class extends DocmanaError {
|
|
228
|
+
errors;
|
|
229
|
+
result;
|
|
230
|
+
constructor(message2, errors = [], result) {
|
|
231
|
+
super(message2, { code: "execution_failed" });
|
|
232
|
+
this.errors = errors;
|
|
233
|
+
this.result = result;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
237
|
+
jobId;
|
|
238
|
+
error;
|
|
239
|
+
result;
|
|
240
|
+
constructor(message2, result) {
|
|
241
|
+
super(message2, { code: "fraud_detection_failed" });
|
|
242
|
+
this.jobId = result.jobId;
|
|
243
|
+
this.error = result.error;
|
|
244
|
+
this.result = result;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
var DocmanaTimeoutError = class extends DocmanaError {
|
|
248
|
+
constructor(message2) {
|
|
249
|
+
super(message2, { code: "timeout" });
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var DocmanaAbortError = class extends DocmanaError {
|
|
253
|
+
constructor(message2) {
|
|
254
|
+
super(message2, { code: "aborted" });
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var DocmanaRateLimitError = class extends DocmanaError {
|
|
258
|
+
retryAfterMs;
|
|
259
|
+
constructor(message2, opts = {}) {
|
|
260
|
+
super(message2, { ...opts, code: "rate_limited" });
|
|
261
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
265
|
+
retryAfterMs;
|
|
266
|
+
constructor(message2, opts = {}) {
|
|
267
|
+
super(message2, { ...opts, code: "service_unavailable" });
|
|
268
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
function errorFromResponse(status, message2, requestId, retryAfterMs) {
|
|
272
|
+
const opts = { status, requestId };
|
|
273
|
+
switch (status) {
|
|
274
|
+
case 400:
|
|
275
|
+
return new DocmanaRequestError(message2, opts);
|
|
276
|
+
case 401:
|
|
277
|
+
return new DocmanaAuthError(message2, opts);
|
|
278
|
+
case 403:
|
|
279
|
+
return new DocmanaPermissionError(message2, opts);
|
|
280
|
+
case 404:
|
|
281
|
+
return new DocmanaNotFoundError(message2, opts);
|
|
282
|
+
case 413:
|
|
283
|
+
return new DocmanaPayloadTooLargeError(message2, opts);
|
|
284
|
+
case 429:
|
|
285
|
+
return new DocmanaRateLimitError(message2, { ...opts, retryAfterMs });
|
|
286
|
+
case 503:
|
|
287
|
+
return new DocmanaServiceUnavailableError(message2, { ...opts, retryAfterMs });
|
|
288
|
+
default:
|
|
289
|
+
return new DocmanaError(message2, opts);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/sdk/review-events.ts
|
|
294
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
295
|
+
var REVIEW_EVENT_TYPES = [
|
|
296
|
+
"execution.awaiting_review",
|
|
297
|
+
"execution.review_done",
|
|
298
|
+
"execution.review_closed",
|
|
299
|
+
"execution.review_expired"
|
|
300
|
+
];
|
|
301
|
+
var ReviewEventVerificationError = class extends Error {
|
|
302
|
+
constructor(reason) {
|
|
303
|
+
super(`Invalid review event ${reason}`);
|
|
304
|
+
this.reason = reason;
|
|
305
|
+
this.name = "ReviewEventVerificationError";
|
|
306
|
+
}
|
|
307
|
+
reason;
|
|
308
|
+
};
|
|
309
|
+
function parseReviewEventCallback(value) {
|
|
310
|
+
const fail2 = () => {
|
|
311
|
+
throw new ReviewEventVerificationError("payload");
|
|
312
|
+
};
|
|
313
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return fail2();
|
|
314
|
+
const raw = value;
|
|
315
|
+
const text2 = (key) => {
|
|
316
|
+
const v = raw[key];
|
|
317
|
+
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
318
|
+
return fail2();
|
|
319
|
+
return v;
|
|
320
|
+
};
|
|
321
|
+
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
322
|
+
return fail2();
|
|
323
|
+
const event = {
|
|
324
|
+
schemaVersion: 1,
|
|
325
|
+
eventId: text2("eventId"),
|
|
326
|
+
type: raw.type,
|
|
327
|
+
organization: text2("organization"),
|
|
328
|
+
executionResultId: text2("executionResultId"),
|
|
329
|
+
reviewId: text2("reviewId"),
|
|
330
|
+
publishedAt: text2("publishedAt")
|
|
331
|
+
};
|
|
332
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
333
|
+
for (const key of ["flowId", "nodeId"])
|
|
334
|
+
if (raw[key] !== void 0) event[key] = text2(key);
|
|
335
|
+
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
336
|
+
if (raw[key] === void 0) continue;
|
|
337
|
+
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail2();
|
|
338
|
+
event[key] = Number(raw[key]);
|
|
339
|
+
}
|
|
340
|
+
if (raw.documentKeys !== void 0) {
|
|
341
|
+
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
342
|
+
return fail2();
|
|
343
|
+
event.documentKeys = raw.documentKeys;
|
|
344
|
+
}
|
|
345
|
+
if (raw.decision !== void 0) {
|
|
346
|
+
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail2();
|
|
347
|
+
event.decision = raw.decision;
|
|
348
|
+
}
|
|
349
|
+
if (raw.reviewStatus !== void 0) {
|
|
350
|
+
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail2();
|
|
351
|
+
event.reviewStatus = raw.reviewStatus;
|
|
352
|
+
}
|
|
353
|
+
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
354
|
+
return fail2();
|
|
355
|
+
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail2();
|
|
356
|
+
return event;
|
|
357
|
+
}
|
|
358
|
+
function verifyReviewEventCallback(options) {
|
|
359
|
+
const { body, signature, timestamp, secret } = options;
|
|
360
|
+
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
361
|
+
throw new ReviewEventVerificationError("timestamp");
|
|
362
|
+
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
363
|
+
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
364
|
+
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
365
|
+
throw new ReviewEventVerificationError("signature");
|
|
366
|
+
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
367
|
+
if (!timingSafeEqual(expected, Buffer.from(signature.slice(3), "hex")))
|
|
368
|
+
throw new ReviewEventVerificationError("signature");
|
|
369
|
+
let value;
|
|
370
|
+
try {
|
|
371
|
+
value = JSON.parse(body);
|
|
372
|
+
} catch {
|
|
373
|
+
throw new ReviewEventVerificationError("payload");
|
|
374
|
+
}
|
|
375
|
+
const event = parseReviewEventCallback(value);
|
|
376
|
+
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
377
|
+
throw new ReviewEventVerificationError("organization");
|
|
378
|
+
return event;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/sdk/resume-progress.ts
|
|
382
|
+
var EXECUTION_PROGRESS_TYPE = "execution.progress";
|
|
383
|
+
var PROGRESS_MESSAGE_KEYS = [
|
|
384
|
+
"resume.running",
|
|
385
|
+
"resume.awaitingReview",
|
|
386
|
+
"resume.completed",
|
|
387
|
+
"resume.rejected",
|
|
388
|
+
"resume.failed",
|
|
389
|
+
"node.pending",
|
|
390
|
+
"node.running",
|
|
391
|
+
"node.done",
|
|
392
|
+
"node.failed",
|
|
393
|
+
"node.reused",
|
|
394
|
+
"node.skipped",
|
|
395
|
+
"item.pending",
|
|
396
|
+
"item.running",
|
|
397
|
+
"item.llmCall",
|
|
398
|
+
"item.llmCallRetry",
|
|
399
|
+
"item.subagent",
|
|
400
|
+
"item.subagentBatch",
|
|
401
|
+
"item.ocrCall",
|
|
402
|
+
"item.done",
|
|
403
|
+
"item.failed",
|
|
404
|
+
"item.reused",
|
|
405
|
+
"item.skippedAncestorFailed",
|
|
406
|
+
"item.skippedBranchNotTaken"
|
|
407
|
+
];
|
|
408
|
+
var fail = () => {
|
|
409
|
+
throw new ReviewEventVerificationError("payload");
|
|
410
|
+
};
|
|
411
|
+
function record(value) {
|
|
412
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : fail();
|
|
413
|
+
}
|
|
414
|
+
function text(value) {
|
|
415
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 && [...value].every((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127) ? value : fail();
|
|
416
|
+
}
|
|
417
|
+
function instant(value) {
|
|
418
|
+
const v = text(value);
|
|
419
|
+
return /^\d{4}-\d{2}-\d{2}T.+(?:Z|[+-]\d{2}:\d{2})$/.test(v) && Number.isFinite(Date.parse(v)) ? v : fail();
|
|
420
|
+
}
|
|
421
|
+
var integer = (v, min = 0) => Number.isSafeInteger(v) && Number(v) >= min ? Number(v) : fail();
|
|
422
|
+
var bool = (v) => typeof v === "boolean" ? v : fail();
|
|
423
|
+
function choice(v, values) {
|
|
424
|
+
return values.includes(v) ? v : fail();
|
|
425
|
+
}
|
|
426
|
+
function array(v, max) {
|
|
427
|
+
return Array.isArray(v) && v.length <= max ? v : fail();
|
|
428
|
+
}
|
|
429
|
+
var state = (v) => choice(v, ["pending", "running", "done", "failed", "skipped", "reused"]);
|
|
430
|
+
function message(value, prefix) {
|
|
431
|
+
const raw = record(value), p = record(raw.params), params = {};
|
|
432
|
+
const key = choice(raw.key, PROGRESS_MESSAGE_KEYS);
|
|
433
|
+
if (!key.startsWith(prefix + ".")) return fail();
|
|
434
|
+
if (prefix !== "resume") params.nodeId = text(p.nodeId);
|
|
435
|
+
if (prefix === "node") {
|
|
436
|
+
params.done = integer(p.done);
|
|
437
|
+
params.total = integer(p.total);
|
|
438
|
+
}
|
|
439
|
+
if (prefix === "item") {
|
|
440
|
+
params.docKey = text(p.docKey);
|
|
441
|
+
params.branchId = p.branchId === null ? null : text(p.branchId);
|
|
442
|
+
for (const k of ["attempt", "batch", "batches"])
|
|
443
|
+
if (p[k] !== void 0) params[k] = integer(p[k], 1);
|
|
444
|
+
}
|
|
445
|
+
return { key, params };
|
|
446
|
+
}
|
|
447
|
+
function parseExecutionProgress(value) {
|
|
448
|
+
const raw = record(value);
|
|
449
|
+
if (raw.schemaVersion !== 1 || raw.type !== EXECUTION_PROGRESS_TYPE) return fail();
|
|
450
|
+
const result = {
|
|
451
|
+
schemaVersion: 1,
|
|
452
|
+
type: EXECUTION_PROGRESS_TYPE,
|
|
453
|
+
eventId: text(raw.eventId),
|
|
454
|
+
organization: text(raw.organization),
|
|
455
|
+
executionResultId: text(raw.executionResultId),
|
|
456
|
+
flowId: text(raw.flowId),
|
|
457
|
+
flowVersion: raw.flowVersion === null ? null : integer(raw.flowVersion, 1),
|
|
458
|
+
reviewId: text(raw.reviewId),
|
|
459
|
+
revision: instant(raw.revision),
|
|
460
|
+
stateVersion: integer(raw.stateVersion, 1),
|
|
461
|
+
phase: choice(raw.phase, [
|
|
462
|
+
"running",
|
|
463
|
+
"awaiting_review",
|
|
464
|
+
"completed",
|
|
465
|
+
"rejected",
|
|
466
|
+
"failed"
|
|
467
|
+
]),
|
|
468
|
+
message: message(raw.message, "resume"),
|
|
469
|
+
updatedAt: instant(raw.updatedAt),
|
|
470
|
+
itemsTruncated: bool(raw.itemsTruncated),
|
|
471
|
+
nodesTruncated: bool(raw.nodesTruncated),
|
|
472
|
+
nodes: array(raw.nodes, 5e3).map((v) => {
|
|
473
|
+
const n = record(v), nodeId = text(n.nodeId), documents = record(n.documents), m = message(n.message, "node");
|
|
474
|
+
if (m.params.nodeId !== nodeId) return fail();
|
|
475
|
+
return {
|
|
476
|
+
nodeId,
|
|
477
|
+
nodeType: text(n.nodeType),
|
|
478
|
+
status: state(n.status),
|
|
479
|
+
message: m,
|
|
480
|
+
documents: {
|
|
481
|
+
total: integer(documents.total),
|
|
482
|
+
pending: integer(documents.pending),
|
|
483
|
+
running: integer(documents.running),
|
|
484
|
+
done: integer(documents.done),
|
|
485
|
+
failed: integer(documents.failed),
|
|
486
|
+
skipped: integer(documents.skipped),
|
|
487
|
+
reused: integer(documents.reused)
|
|
488
|
+
},
|
|
489
|
+
items: array(n.items, 1e4).map((v2) => {
|
|
490
|
+
const i = record(v2), a = i.activity === null ? null : record(i.activity), m2 = message(i.message, "item");
|
|
491
|
+
const documentKey = text(i.documentKey), branchId = i.branchId === null ? null : text(i.branchId);
|
|
492
|
+
if (m2.params.nodeId !== nodeId || m2.params.docKey !== documentKey || m2.params.branchId !== branchId)
|
|
493
|
+
return fail();
|
|
494
|
+
return {
|
|
495
|
+
documentKey,
|
|
496
|
+
branchId,
|
|
497
|
+
status: state(i.status),
|
|
498
|
+
message: m2,
|
|
499
|
+
startedAt: i.startedAt === null ? null : instant(i.startedAt),
|
|
500
|
+
endedAt: i.endedAt === null ? null : instant(i.endedAt),
|
|
501
|
+
activity: a === null ? null : {
|
|
502
|
+
kind: choice(a.kind, ["llm_call", "subagent", "ocr_call"]),
|
|
503
|
+
attempt: a.attempt === null ? null : integer(a.attempt, 1),
|
|
504
|
+
batch: a.batch === null ? null : integer(a.batch, 1),
|
|
505
|
+
batches: a.batches === null ? null : integer(a.batches, 1)
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
})
|
|
509
|
+
};
|
|
510
|
+
}),
|
|
511
|
+
...raw.publishedAt === void 0 ? {} : { publishedAt: instant(raw.publishedAt) }
|
|
512
|
+
};
|
|
513
|
+
const phaseKey = result.phase === "awaiting_review" ? "awaitingReview" : result.phase;
|
|
514
|
+
if (result.message.key !== `resume.${phaseKey}` || result.eventId !== `${result.executionResultId}:${result.revision}:${result.stateVersion}`)
|
|
515
|
+
return fail();
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
function parseResumeProgressResponse(value) {
|
|
519
|
+
const event = parseExecutionProgress(value), labels = record(record(value).labels);
|
|
520
|
+
const label = (v) => {
|
|
521
|
+
if (v === null) return null;
|
|
522
|
+
if (typeof v !== "string") return fail();
|
|
523
|
+
return [...v.replace(/\s+/g, " ")].filter((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127).join("").trim().slice(0, 512) || null;
|
|
524
|
+
};
|
|
525
|
+
return {
|
|
526
|
+
...event,
|
|
527
|
+
labels: {
|
|
528
|
+
nodes: Object.fromEntries(
|
|
529
|
+
Object.entries(record(labels.nodes)).map(([k, v]) => {
|
|
530
|
+
const n = record(v);
|
|
531
|
+
return [text(k), { name: label(n.name), type: text(n.type) }];
|
|
532
|
+
})
|
|
533
|
+
),
|
|
534
|
+
documents: Object.fromEntries(
|
|
535
|
+
Object.entries(record(labels.documents)).map(([k, v]) => [text(k), label(v)])
|
|
536
|
+
),
|
|
537
|
+
branches: Object.fromEntries(
|
|
538
|
+
Object.entries(record(labels.branches)).map(([k, v]) => [text(k), label(v)])
|
|
539
|
+
)
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
185
544
|
// src/sdk/reviews.ts
|
|
186
545
|
function segment(value) {
|
|
187
546
|
if (!value || value === "." || value === ".." || /[\\/]/.test(value) || [...value].some((char) => char.charCodeAt(0) < 32))
|
|
@@ -193,6 +552,38 @@ var Reviews = class {
|
|
|
193
552
|
this.http = http;
|
|
194
553
|
}
|
|
195
554
|
http;
|
|
555
|
+
/** One non-consuming snapshot read for opening/reconnecting a view. No polling. */
|
|
556
|
+
async progress(executionId, options) {
|
|
557
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
558
|
+
const response = await this.http.requestRaw(
|
|
559
|
+
"GET",
|
|
560
|
+
`/executions/${segment(executionId)}/review/progress`,
|
|
561
|
+
{
|
|
562
|
+
signal: options.signal,
|
|
563
|
+
query: { organization: options.organization }
|
|
564
|
+
}
|
|
565
|
+
);
|
|
566
|
+
let body;
|
|
567
|
+
try {
|
|
568
|
+
body = await response.text();
|
|
569
|
+
} catch (error) {
|
|
570
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
571
|
+
throw new DocmanaAbortError("Request aborted");
|
|
572
|
+
throw error;
|
|
573
|
+
}
|
|
574
|
+
try {
|
|
575
|
+
const result = parseResumeProgressResponse(JSON.parse(body));
|
|
576
|
+
if (result.executionResultId !== executionId || result.organization !== options.organization)
|
|
577
|
+
throw new Error("Invalid progress scope");
|
|
578
|
+
return result;
|
|
579
|
+
} catch {
|
|
580
|
+
throw new DocmanaError("Invalid execution progress response", {
|
|
581
|
+
status: response.status,
|
|
582
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
583
|
+
code: "invalid_progress_response"
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
}
|
|
196
587
|
/** Read a page of HITL execution metadata without consuming any result. */
|
|
197
588
|
listExecutions(options) {
|
|
198
589
|
return this.http.requestJson("GET", "/reviews/executions", {
|
|
@@ -275,105 +666,69 @@ var Reviews = class {
|
|
|
275
666
|
query: {
|
|
276
667
|
organization: options.organization,
|
|
277
668
|
...options.version ? { v: options.version } : {},
|
|
278
|
-
...options.size ? { size: options.size } : {}
|
|
669
|
+
...options.size ? { size: options.size } : {},
|
|
670
|
+
...options.language ? { language: options.language } : {}
|
|
279
671
|
}
|
|
280
672
|
}
|
|
281
673
|
);
|
|
282
674
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
675
|
+
async requestDocumentTranslation(executionId, docKey, language, options) {
|
|
676
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
677
|
+
if (!language?.trim()) throw new Error("Language is required");
|
|
678
|
+
const response = await this.http.requestRaw(
|
|
679
|
+
"POST",
|
|
680
|
+
`/executions/${segment(executionId)}/review/documents/${segment(docKey)}/translations`,
|
|
681
|
+
{
|
|
682
|
+
signal: options.signal,
|
|
683
|
+
query: { organization: options.organization },
|
|
684
|
+
headers: { "content-type": "application/json" },
|
|
685
|
+
body: JSON.stringify({ language })
|
|
686
|
+
}
|
|
687
|
+
);
|
|
688
|
+
let body;
|
|
689
|
+
try {
|
|
690
|
+
body = await response.json();
|
|
691
|
+
} catch (error) {
|
|
692
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
693
|
+
throw new DocmanaAbortError("Request aborted");
|
|
694
|
+
throw new DocmanaError("Invalid document translation response", {
|
|
695
|
+
status: response.status,
|
|
696
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
697
|
+
code: "invalid_translation_response"
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
return parseDocumentTranslation(body, response.status === 202 ? 202 : 200, response);
|
|
301
701
|
}
|
|
302
|
-
reason;
|
|
303
702
|
};
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
703
|
+
var VARIANTS = ["original", "translation"];
|
|
704
|
+
var TRANSLATION_STATES = [
|
|
705
|
+
"absent",
|
|
706
|
+
"preparing",
|
|
707
|
+
"ready",
|
|
708
|
+
"failed",
|
|
709
|
+
"unsupported"
|
|
710
|
+
];
|
|
711
|
+
function parseDocumentTranslation(body, status, response) {
|
|
712
|
+
const data = typeof body === "object" && body !== null ? body : {};
|
|
713
|
+
const servedLanguage = data.servedLanguage;
|
|
714
|
+
if (typeof data.requestedLanguage !== "string" || !(servedLanguage === null || typeof servedLanguage === "string") || !VARIANTS.includes(data.variant) || typeof data.fallback !== "boolean" || !TRANSLATION_STATES.includes(data.translationState))
|
|
715
|
+
throw new DocmanaError("Invalid document translation response", {
|
|
716
|
+
status: response.status,
|
|
717
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
718
|
+
code: "invalid_translation_response"
|
|
719
|
+
});
|
|
720
|
+
return {
|
|
721
|
+
status,
|
|
722
|
+
requestedLanguage: data.requestedLanguage,
|
|
723
|
+
servedLanguage,
|
|
724
|
+
variant: data.variant,
|
|
725
|
+
fallback: data.fallback,
|
|
726
|
+
translationState: data.translationState
|
|
326
727
|
};
|
|
327
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
328
|
-
for (const key of ["flowId", "nodeId"])
|
|
329
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
330
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
331
|
-
if (raw[key] === void 0) continue;
|
|
332
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
333
|
-
event[key] = Number(raw[key]);
|
|
334
|
-
}
|
|
335
|
-
if (raw.documentKeys !== void 0) {
|
|
336
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
337
|
-
return fail();
|
|
338
|
-
event.documentKeys = raw.documentKeys;
|
|
339
|
-
}
|
|
340
|
-
if (raw.decision !== void 0) {
|
|
341
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
342
|
-
event.decision = raw.decision;
|
|
343
|
-
}
|
|
344
|
-
if (raw.reviewStatus !== void 0) {
|
|
345
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
346
|
-
event.reviewStatus = raw.reviewStatus;
|
|
347
|
-
}
|
|
348
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
349
|
-
return fail();
|
|
350
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
351
|
-
return event;
|
|
352
|
-
}
|
|
353
|
-
function verifyReviewEventCallback(options) {
|
|
354
|
-
const { body, signature, timestamp, secret } = options;
|
|
355
|
-
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
356
|
-
throw new ReviewEventVerificationError("timestamp");
|
|
357
|
-
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
358
|
-
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
359
|
-
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
360
|
-
throw new ReviewEventVerificationError("signature");
|
|
361
|
-
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
362
|
-
if (!timingSafeEqual(expected, Buffer.from(signature.slice(3), "hex")))
|
|
363
|
-
throw new ReviewEventVerificationError("signature");
|
|
364
|
-
let value;
|
|
365
|
-
try {
|
|
366
|
-
value = JSON.parse(body);
|
|
367
|
-
} catch {
|
|
368
|
-
throw new ReviewEventVerificationError("payload");
|
|
369
|
-
}
|
|
370
|
-
const event = parseReviewEventCallback(value);
|
|
371
|
-
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
372
|
-
throw new ReviewEventVerificationError("organization");
|
|
373
|
-
return event;
|
|
374
728
|
}
|
|
375
729
|
|
|
376
730
|
// src/sdk/execution-events.ts
|
|
731
|
+
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
377
732
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
378
733
|
"execution.node.started",
|
|
379
734
|
"execution.node.completed",
|
|
@@ -382,42 +737,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
382
737
|
];
|
|
383
738
|
function parseExecutionEventCallback(value) {
|
|
384
739
|
const raw = value;
|
|
740
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
385
741
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
386
742
|
return parseReviewEventCallback(value);
|
|
387
|
-
const
|
|
743
|
+
const fail2 = () => {
|
|
388
744
|
throw new ReviewEventVerificationError("payload");
|
|
389
745
|
};
|
|
390
|
-
const
|
|
746
|
+
const text2 = (key) => {
|
|
391
747
|
const v = raw[key];
|
|
392
|
-
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v :
|
|
748
|
+
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v : fail2();
|
|
393
749
|
};
|
|
394
|
-
const
|
|
395
|
-
if (raw.schemaVersion !== 1) return
|
|
750
|
+
const integer2 = (key) => Number.isSafeInteger(raw[key]) && Number(raw[key]) >= 1 ? Number(raw[key]) : fail2();
|
|
751
|
+
if (raw.schemaVersion !== 1) return fail2();
|
|
396
752
|
const event = {
|
|
397
753
|
schemaVersion: 1,
|
|
398
|
-
eventId:
|
|
754
|
+
eventId: text2("eventId"),
|
|
399
755
|
type: raw.type,
|
|
400
|
-
organization:
|
|
401
|
-
executionResultId:
|
|
402
|
-
sequence:
|
|
403
|
-
occurredAt:
|
|
404
|
-
nodeId:
|
|
405
|
-
nodeType:
|
|
406
|
-
revision:
|
|
407
|
-
attemptId:
|
|
756
|
+
organization: text2("organization"),
|
|
757
|
+
executionResultId: text2("executionResultId"),
|
|
758
|
+
sequence: integer2("sequence"),
|
|
759
|
+
occurredAt: text2("occurredAt"),
|
|
760
|
+
nodeId: text2("nodeId"),
|
|
761
|
+
nodeType: text2("nodeType"),
|
|
762
|
+
revision: integer2("revision"),
|
|
763
|
+
attemptId: text2("attemptId"),
|
|
408
764
|
documentKeys: []
|
|
409
765
|
};
|
|
410
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
766
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
411
767
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
412
768
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
413
769
|
))
|
|
414
|
-
return
|
|
770
|
+
return fail2();
|
|
415
771
|
event.documentKeys = raw.documentKeys;
|
|
416
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
417
|
-
if (raw.flowVersion !== void 0) event.flowVersion =
|
|
772
|
+
if (raw.flowId !== void 0) event.flowId = text2("flowId");
|
|
773
|
+
if (raw.flowVersion !== void 0) event.flowVersion = integer2("flowVersion");
|
|
418
774
|
if (raw.publishedAt !== void 0) {
|
|
419
|
-
event.publishedAt =
|
|
420
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
775
|
+
event.publishedAt = text2("publishedAt");
|
|
776
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
421
777
|
}
|
|
422
778
|
return event;
|
|
423
779
|
}
|
|
@@ -472,114 +828,6 @@ var ExecutionEvents = class {
|
|
|
472
828
|
}
|
|
473
829
|
};
|
|
474
830
|
|
|
475
|
-
// src/sdk/errors.ts
|
|
476
|
-
var DocmanaError = class extends Error {
|
|
477
|
-
status;
|
|
478
|
-
requestId;
|
|
479
|
-
code;
|
|
480
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
481
|
-
apiCode;
|
|
482
|
-
reviewConflict;
|
|
483
|
-
constructor(message, opts = {}) {
|
|
484
|
-
super(message);
|
|
485
|
-
this.name = new.target.name;
|
|
486
|
-
this.status = opts.status;
|
|
487
|
-
this.requestId = opts.requestId;
|
|
488
|
-
this.code = opts.code ?? "docmana_error";
|
|
489
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
493
|
-
constructor(message, opts = {}) {
|
|
494
|
-
super(message, { ...opts, code: "auth_error" });
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
498
|
-
constructor(message, opts = {}) {
|
|
499
|
-
super(message, { ...opts, code: "permission_error" });
|
|
500
|
-
}
|
|
501
|
-
};
|
|
502
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
503
|
-
constructor(message, opts = {}) {
|
|
504
|
-
super(message, { ...opts, code: "not_found" });
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
508
|
-
constructor(message, opts = {}) {
|
|
509
|
-
super(message, { ...opts, code: "bad_request" });
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
513
|
-
constructor(message, opts = {}) {
|
|
514
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
518
|
-
errors;
|
|
519
|
-
result;
|
|
520
|
-
constructor(message, errors = [], result) {
|
|
521
|
-
super(message, { code: "execution_failed" });
|
|
522
|
-
this.errors = errors;
|
|
523
|
-
this.result = result;
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
527
|
-
jobId;
|
|
528
|
-
error;
|
|
529
|
-
result;
|
|
530
|
-
constructor(message, result) {
|
|
531
|
-
super(message, { code: "fraud_detection_failed" });
|
|
532
|
-
this.jobId = result.jobId;
|
|
533
|
-
this.error = result.error;
|
|
534
|
-
this.result = result;
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
538
|
-
constructor(message) {
|
|
539
|
-
super(message, { code: "timeout" });
|
|
540
|
-
}
|
|
541
|
-
};
|
|
542
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
543
|
-
constructor(message) {
|
|
544
|
-
super(message, { code: "aborted" });
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
548
|
-
retryAfterMs;
|
|
549
|
-
constructor(message, opts = {}) {
|
|
550
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
551
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
555
|
-
retryAfterMs;
|
|
556
|
-
constructor(message, opts = {}) {
|
|
557
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
558
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
562
|
-
const opts = { status, requestId };
|
|
563
|
-
switch (status) {
|
|
564
|
-
case 400:
|
|
565
|
-
return new DocmanaRequestError(message, opts);
|
|
566
|
-
case 401:
|
|
567
|
-
return new DocmanaAuthError(message, opts);
|
|
568
|
-
case 403:
|
|
569
|
-
return new DocmanaPermissionError(message, opts);
|
|
570
|
-
case 404:
|
|
571
|
-
return new DocmanaNotFoundError(message, opts);
|
|
572
|
-
case 413:
|
|
573
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
574
|
-
case 429:
|
|
575
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
576
|
-
case 503:
|
|
577
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
578
|
-
default:
|
|
579
|
-
return new DocmanaError(message, opts);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
831
|
// src/sdk/http/http-client.ts
|
|
584
832
|
var HttpClient = class {
|
|
585
833
|
constructor(opts) {
|
|
@@ -588,10 +836,10 @@ var HttpClient = class {
|
|
|
588
836
|
opts;
|
|
589
837
|
async requestJson(method, path, options = {}) {
|
|
590
838
|
const res = await this.requestRaw(method, path, options);
|
|
591
|
-
const
|
|
592
|
-
if (!
|
|
839
|
+
const text2 = await res.text();
|
|
840
|
+
if (!text2) return {};
|
|
593
841
|
try {
|
|
594
|
-
return JSON.parse(
|
|
842
|
+
return JSON.parse(text2);
|
|
595
843
|
} catch {
|
|
596
844
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
597
845
|
}
|
|
@@ -607,10 +855,10 @@ var HttpClient = class {
|
|
|
607
855
|
}
|
|
608
856
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
609
857
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
610
|
-
const
|
|
858
|
+
const message2 = await this.extractMessage(res);
|
|
611
859
|
const error = errorFromResponse(
|
|
612
860
|
res.status,
|
|
613
|
-
|
|
861
|
+
message2,
|
|
614
862
|
requestId,
|
|
615
863
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
616
864
|
);
|
|
@@ -669,15 +917,15 @@ function isRecord(value) {
|
|
|
669
917
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
670
918
|
}
|
|
671
919
|
function reviewConflictDetails(raw) {
|
|
672
|
-
const
|
|
920
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
673
921
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
674
922
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
675
|
-
const id =
|
|
676
|
-
const name =
|
|
923
|
+
const id = text2(actor.id, 200);
|
|
924
|
+
const name = text2(actor.name, 200);
|
|
677
925
|
const profile = {
|
|
678
|
-
firstName:
|
|
679
|
-
lastName:
|
|
680
|
-
email:
|
|
926
|
+
firstName: text2(actor.firstName, 200),
|
|
927
|
+
lastName: text2(actor.lastName, 200),
|
|
928
|
+
email: text2(actor.email, 200)
|
|
681
929
|
};
|
|
682
930
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
683
931
|
const decidedBy = [
|
|
@@ -697,13 +945,13 @@ function reviewConflictDetails(raw) {
|
|
|
697
945
|
}
|
|
698
946
|
}
|
|
699
947
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
700
|
-
const decidedAt =
|
|
948
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
701
949
|
return {
|
|
702
950
|
currentVersion,
|
|
703
951
|
decidedBy,
|
|
704
952
|
decidedAt,
|
|
705
953
|
version: currentVersion,
|
|
706
|
-
submittedBy: id ?? name ??
|
|
954
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
707
955
|
submittedAt: decidedAt
|
|
708
956
|
};
|
|
709
957
|
}
|
|
@@ -905,8 +1153,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
905
1153
|
query: organization ? { organization } : void 0,
|
|
906
1154
|
signal
|
|
907
1155
|
});
|
|
908
|
-
const
|
|
909
|
-
const body =
|
|
1156
|
+
const text2 = await res.text();
|
|
1157
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
910
1158
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
911
1159
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
912
1160
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -974,8 +1222,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
974
1222
|
signal
|
|
975
1223
|
}
|
|
976
1224
|
);
|
|
977
|
-
const
|
|
978
|
-
const body =
|
|
1225
|
+
const text2 = await res.text();
|
|
1226
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
979
1227
|
const normalized = {
|
|
980
1228
|
...body,
|
|
981
1229
|
status: normalizeStatus(body.status)
|
|
@@ -1370,12 +1618,16 @@ export {
|
|
|
1370
1618
|
DocmanaServiceUnavailableError,
|
|
1371
1619
|
DocmanaTimeoutError,
|
|
1372
1620
|
EXECUTION_NODE_EVENT_TYPES,
|
|
1621
|
+
EXECUTION_PROGRESS_TYPE,
|
|
1373
1622
|
ExecutionEvents,
|
|
1374
1623
|
FormulaAuthoring,
|
|
1624
|
+
PROGRESS_MESSAGE_KEYS,
|
|
1375
1625
|
REVIEW_EVENT_TYPES,
|
|
1376
1626
|
ReviewEventVerificationError,
|
|
1377
1627
|
Reviews,
|
|
1378
1628
|
parseExecutionEventCallback,
|
|
1629
|
+
parseExecutionProgress,
|
|
1630
|
+
parseResumeProgressResponse,
|
|
1379
1631
|
parseReviewEventCallback,
|
|
1380
1632
|
verifyExecutionEventCallback,
|
|
1381
1633
|
verifyReviewEventCallback
|