@docmana/sdk 0.46.0 → 0.47.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 +9 -0
- package/LICENSE +21 -21
- package/README.md +23 -0
- package/dist/cli.mjs +413 -219
- package/dist/cli.mjs.map +1 -1
- package/dist/execution-events.d.ts +2 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +437 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +433 -235
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +11 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -33,12 +33,16 @@ __export(sdk_exports, {
|
|
|
33
33
|
DocmanaServiceUnavailableError: () => DocmanaServiceUnavailableError,
|
|
34
34
|
DocmanaTimeoutError: () => DocmanaTimeoutError,
|
|
35
35
|
EXECUTION_NODE_EVENT_TYPES: () => EXECUTION_NODE_EVENT_TYPES,
|
|
36
|
+
EXECUTION_PROGRESS_TYPE: () => EXECUTION_PROGRESS_TYPE,
|
|
36
37
|
ExecutionEvents: () => ExecutionEvents,
|
|
37
38
|
FormulaAuthoring: () => FormulaAuthoring,
|
|
39
|
+
PROGRESS_MESSAGE_KEYS: () => PROGRESS_MESSAGE_KEYS,
|
|
38
40
|
REVIEW_EVENT_TYPES: () => REVIEW_EVENT_TYPES,
|
|
39
41
|
ReviewEventVerificationError: () => ReviewEventVerificationError,
|
|
40
42
|
Reviews: () => Reviews,
|
|
41
43
|
parseExecutionEventCallback: () => parseExecutionEventCallback,
|
|
44
|
+
parseExecutionProgress: () => parseExecutionProgress,
|
|
45
|
+
parseResumeProgressResponse: () => parseResumeProgressResponse,
|
|
42
46
|
parseReviewEventCallback: () => parseReviewEventCallback,
|
|
43
47
|
verifyExecutionEventCallback: () => verifyExecutionEventCallback,
|
|
44
48
|
verifyReviewEventCallback: () => verifyReviewEventCallback
|
|
@@ -229,6 +233,365 @@ var FormulaAuthoring = class {
|
|
|
229
233
|
}
|
|
230
234
|
};
|
|
231
235
|
|
|
236
|
+
// src/sdk/errors.ts
|
|
237
|
+
var DocmanaError = class extends Error {
|
|
238
|
+
status;
|
|
239
|
+
requestId;
|
|
240
|
+
code;
|
|
241
|
+
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
242
|
+
apiCode;
|
|
243
|
+
reviewConflict;
|
|
244
|
+
constructor(message2, opts = {}) {
|
|
245
|
+
super(message2);
|
|
246
|
+
this.name = new.target.name;
|
|
247
|
+
this.status = opts.status;
|
|
248
|
+
this.requestId = opts.requestId;
|
|
249
|
+
this.code = opts.code ?? "docmana_error";
|
|
250
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
var DocmanaAuthError = class extends DocmanaError {
|
|
254
|
+
constructor(message2, opts = {}) {
|
|
255
|
+
super(message2, { ...opts, code: "auth_error" });
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
var DocmanaPermissionError = class extends DocmanaError {
|
|
259
|
+
constructor(message2, opts = {}) {
|
|
260
|
+
super(message2, { ...opts, code: "permission_error" });
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var DocmanaNotFoundError = class extends DocmanaError {
|
|
264
|
+
constructor(message2, opts = {}) {
|
|
265
|
+
super(message2, { ...opts, code: "not_found" });
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
var DocmanaRequestError = class extends DocmanaError {
|
|
269
|
+
constructor(message2, opts = {}) {
|
|
270
|
+
super(message2, { ...opts, code: "bad_request" });
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
274
|
+
constructor(message2, opts = {}) {
|
|
275
|
+
super(message2, { ...opts, code: "payload_too_large" });
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
var DocmanaExecutionError = class extends DocmanaError {
|
|
279
|
+
errors;
|
|
280
|
+
result;
|
|
281
|
+
constructor(message2, errors = [], result) {
|
|
282
|
+
super(message2, { code: "execution_failed" });
|
|
283
|
+
this.errors = errors;
|
|
284
|
+
this.result = result;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
288
|
+
jobId;
|
|
289
|
+
error;
|
|
290
|
+
result;
|
|
291
|
+
constructor(message2, result) {
|
|
292
|
+
super(message2, { code: "fraud_detection_failed" });
|
|
293
|
+
this.jobId = result.jobId;
|
|
294
|
+
this.error = result.error;
|
|
295
|
+
this.result = result;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
var DocmanaTimeoutError = class extends DocmanaError {
|
|
299
|
+
constructor(message2) {
|
|
300
|
+
super(message2, { code: "timeout" });
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
var DocmanaAbortError = class extends DocmanaError {
|
|
304
|
+
constructor(message2) {
|
|
305
|
+
super(message2, { code: "aborted" });
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
var DocmanaRateLimitError = class extends DocmanaError {
|
|
309
|
+
retryAfterMs;
|
|
310
|
+
constructor(message2, opts = {}) {
|
|
311
|
+
super(message2, { ...opts, code: "rate_limited" });
|
|
312
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
316
|
+
retryAfterMs;
|
|
317
|
+
constructor(message2, opts = {}) {
|
|
318
|
+
super(message2, { ...opts, code: "service_unavailable" });
|
|
319
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
function errorFromResponse(status, message2, requestId, retryAfterMs) {
|
|
323
|
+
const opts = { status, requestId };
|
|
324
|
+
switch (status) {
|
|
325
|
+
case 400:
|
|
326
|
+
return new DocmanaRequestError(message2, opts);
|
|
327
|
+
case 401:
|
|
328
|
+
return new DocmanaAuthError(message2, opts);
|
|
329
|
+
case 403:
|
|
330
|
+
return new DocmanaPermissionError(message2, opts);
|
|
331
|
+
case 404:
|
|
332
|
+
return new DocmanaNotFoundError(message2, opts);
|
|
333
|
+
case 413:
|
|
334
|
+
return new DocmanaPayloadTooLargeError(message2, opts);
|
|
335
|
+
case 429:
|
|
336
|
+
return new DocmanaRateLimitError(message2, { ...opts, retryAfterMs });
|
|
337
|
+
case 503:
|
|
338
|
+
return new DocmanaServiceUnavailableError(message2, { ...opts, retryAfterMs });
|
|
339
|
+
default:
|
|
340
|
+
return new DocmanaError(message2, opts);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// src/sdk/review-events.ts
|
|
345
|
+
var import_node_crypto = require("crypto");
|
|
346
|
+
var REVIEW_EVENT_TYPES = [
|
|
347
|
+
"execution.awaiting_review",
|
|
348
|
+
"execution.review_done",
|
|
349
|
+
"execution.review_closed",
|
|
350
|
+
"execution.review_expired"
|
|
351
|
+
];
|
|
352
|
+
var ReviewEventVerificationError = class extends Error {
|
|
353
|
+
constructor(reason) {
|
|
354
|
+
super(`Invalid review event ${reason}`);
|
|
355
|
+
this.reason = reason;
|
|
356
|
+
this.name = "ReviewEventVerificationError";
|
|
357
|
+
}
|
|
358
|
+
reason;
|
|
359
|
+
};
|
|
360
|
+
function parseReviewEventCallback(value) {
|
|
361
|
+
const fail2 = () => {
|
|
362
|
+
throw new ReviewEventVerificationError("payload");
|
|
363
|
+
};
|
|
364
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return fail2();
|
|
365
|
+
const raw = value;
|
|
366
|
+
const text2 = (key) => {
|
|
367
|
+
const v = raw[key];
|
|
368
|
+
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
369
|
+
return fail2();
|
|
370
|
+
return v;
|
|
371
|
+
};
|
|
372
|
+
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
373
|
+
return fail2();
|
|
374
|
+
const event = {
|
|
375
|
+
schemaVersion: 1,
|
|
376
|
+
eventId: text2("eventId"),
|
|
377
|
+
type: raw.type,
|
|
378
|
+
organization: text2("organization"),
|
|
379
|
+
executionResultId: text2("executionResultId"),
|
|
380
|
+
reviewId: text2("reviewId"),
|
|
381
|
+
publishedAt: text2("publishedAt")
|
|
382
|
+
};
|
|
383
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
384
|
+
for (const key of ["flowId", "nodeId"])
|
|
385
|
+
if (raw[key] !== void 0) event[key] = text2(key);
|
|
386
|
+
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
387
|
+
if (raw[key] === void 0) continue;
|
|
388
|
+
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail2();
|
|
389
|
+
event[key] = Number(raw[key]);
|
|
390
|
+
}
|
|
391
|
+
if (raw.documentKeys !== void 0) {
|
|
392
|
+
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
393
|
+
return fail2();
|
|
394
|
+
event.documentKeys = raw.documentKeys;
|
|
395
|
+
}
|
|
396
|
+
if (raw.decision !== void 0) {
|
|
397
|
+
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail2();
|
|
398
|
+
event.decision = raw.decision;
|
|
399
|
+
}
|
|
400
|
+
if (raw.reviewStatus !== void 0) {
|
|
401
|
+
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail2();
|
|
402
|
+
event.reviewStatus = raw.reviewStatus;
|
|
403
|
+
}
|
|
404
|
+
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
405
|
+
return fail2();
|
|
406
|
+
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail2();
|
|
407
|
+
return event;
|
|
408
|
+
}
|
|
409
|
+
function verifyReviewEventCallback(options) {
|
|
410
|
+
const { body, signature, timestamp, secret } = options;
|
|
411
|
+
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
412
|
+
throw new ReviewEventVerificationError("timestamp");
|
|
413
|
+
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
414
|
+
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
415
|
+
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
416
|
+
throw new ReviewEventVerificationError("signature");
|
|
417
|
+
const expected = (0, import_node_crypto.createHmac)("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
418
|
+
if (!(0, import_node_crypto.timingSafeEqual)(expected, Buffer.from(signature.slice(3), "hex")))
|
|
419
|
+
throw new ReviewEventVerificationError("signature");
|
|
420
|
+
let value;
|
|
421
|
+
try {
|
|
422
|
+
value = JSON.parse(body);
|
|
423
|
+
} catch {
|
|
424
|
+
throw new ReviewEventVerificationError("payload");
|
|
425
|
+
}
|
|
426
|
+
const event = parseReviewEventCallback(value);
|
|
427
|
+
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
428
|
+
throw new ReviewEventVerificationError("organization");
|
|
429
|
+
return event;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/sdk/resume-progress.ts
|
|
433
|
+
var EXECUTION_PROGRESS_TYPE = "execution.progress";
|
|
434
|
+
var PROGRESS_MESSAGE_KEYS = [
|
|
435
|
+
"resume.running",
|
|
436
|
+
"resume.awaitingReview",
|
|
437
|
+
"resume.completed",
|
|
438
|
+
"resume.rejected",
|
|
439
|
+
"resume.failed",
|
|
440
|
+
"node.pending",
|
|
441
|
+
"node.running",
|
|
442
|
+
"node.done",
|
|
443
|
+
"node.failed",
|
|
444
|
+
"node.reused",
|
|
445
|
+
"node.skipped",
|
|
446
|
+
"item.pending",
|
|
447
|
+
"item.running",
|
|
448
|
+
"item.llmCall",
|
|
449
|
+
"item.llmCallRetry",
|
|
450
|
+
"item.subagent",
|
|
451
|
+
"item.subagentBatch",
|
|
452
|
+
"item.ocrCall",
|
|
453
|
+
"item.done",
|
|
454
|
+
"item.failed",
|
|
455
|
+
"item.reused",
|
|
456
|
+
"item.skippedAncestorFailed",
|
|
457
|
+
"item.skippedBranchNotTaken"
|
|
458
|
+
];
|
|
459
|
+
var fail = () => {
|
|
460
|
+
throw new ReviewEventVerificationError("payload");
|
|
461
|
+
};
|
|
462
|
+
function record(value) {
|
|
463
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : fail();
|
|
464
|
+
}
|
|
465
|
+
function text(value) {
|
|
466
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 && [...value].every((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127) ? value : fail();
|
|
467
|
+
}
|
|
468
|
+
function instant(value) {
|
|
469
|
+
const v = text(value);
|
|
470
|
+
return /^\d{4}-\d{2}-\d{2}T.+(?:Z|[+-]\d{2}:\d{2})$/.test(v) && Number.isFinite(Date.parse(v)) ? v : fail();
|
|
471
|
+
}
|
|
472
|
+
var integer = (v, min = 0) => Number.isSafeInteger(v) && Number(v) >= min ? Number(v) : fail();
|
|
473
|
+
var bool = (v) => typeof v === "boolean" ? v : fail();
|
|
474
|
+
function choice(v, values) {
|
|
475
|
+
return values.includes(v) ? v : fail();
|
|
476
|
+
}
|
|
477
|
+
function array(v, max) {
|
|
478
|
+
return Array.isArray(v) && v.length <= max ? v : fail();
|
|
479
|
+
}
|
|
480
|
+
var state = (v) => choice(v, ["pending", "running", "done", "failed", "skipped", "reused"]);
|
|
481
|
+
function message(value, prefix) {
|
|
482
|
+
const raw = record(value), p = record(raw.params), params = {};
|
|
483
|
+
const key = choice(raw.key, PROGRESS_MESSAGE_KEYS);
|
|
484
|
+
if (!key.startsWith(prefix + ".")) return fail();
|
|
485
|
+
if (prefix !== "resume") params.nodeId = text(p.nodeId);
|
|
486
|
+
if (prefix === "node") {
|
|
487
|
+
params.done = integer(p.done);
|
|
488
|
+
params.total = integer(p.total);
|
|
489
|
+
}
|
|
490
|
+
if (prefix === "item") {
|
|
491
|
+
params.docKey = text(p.docKey);
|
|
492
|
+
params.branchId = p.branchId === null ? null : text(p.branchId);
|
|
493
|
+
for (const k of ["attempt", "batch", "batches"])
|
|
494
|
+
if (p[k] !== void 0) params[k] = integer(p[k], 1);
|
|
495
|
+
}
|
|
496
|
+
return { key, params };
|
|
497
|
+
}
|
|
498
|
+
function parseExecutionProgress(value) {
|
|
499
|
+
const raw = record(value);
|
|
500
|
+
if (raw.schemaVersion !== 1 || raw.type !== EXECUTION_PROGRESS_TYPE) return fail();
|
|
501
|
+
const result = {
|
|
502
|
+
schemaVersion: 1,
|
|
503
|
+
type: EXECUTION_PROGRESS_TYPE,
|
|
504
|
+
eventId: text(raw.eventId),
|
|
505
|
+
organization: text(raw.organization),
|
|
506
|
+
executionResultId: text(raw.executionResultId),
|
|
507
|
+
flowId: text(raw.flowId),
|
|
508
|
+
flowVersion: raw.flowVersion === null ? null : integer(raw.flowVersion, 1),
|
|
509
|
+
reviewId: text(raw.reviewId),
|
|
510
|
+
revision: instant(raw.revision),
|
|
511
|
+
stateVersion: integer(raw.stateVersion, 1),
|
|
512
|
+
phase: choice(raw.phase, [
|
|
513
|
+
"running",
|
|
514
|
+
"awaiting_review",
|
|
515
|
+
"completed",
|
|
516
|
+
"rejected",
|
|
517
|
+
"failed"
|
|
518
|
+
]),
|
|
519
|
+
message: message(raw.message, "resume"),
|
|
520
|
+
updatedAt: instant(raw.updatedAt),
|
|
521
|
+
itemsTruncated: bool(raw.itemsTruncated),
|
|
522
|
+
nodesTruncated: bool(raw.nodesTruncated),
|
|
523
|
+
nodes: array(raw.nodes, 5e3).map((v) => {
|
|
524
|
+
const n = record(v), nodeId = text(n.nodeId), documents = record(n.documents), m = message(n.message, "node");
|
|
525
|
+
if (m.params.nodeId !== nodeId) return fail();
|
|
526
|
+
return {
|
|
527
|
+
nodeId,
|
|
528
|
+
nodeType: text(n.nodeType),
|
|
529
|
+
status: state(n.status),
|
|
530
|
+
message: m,
|
|
531
|
+
documents: {
|
|
532
|
+
total: integer(documents.total),
|
|
533
|
+
pending: integer(documents.pending),
|
|
534
|
+
running: integer(documents.running),
|
|
535
|
+
done: integer(documents.done),
|
|
536
|
+
failed: integer(documents.failed),
|
|
537
|
+
skipped: integer(documents.skipped),
|
|
538
|
+
reused: integer(documents.reused)
|
|
539
|
+
},
|
|
540
|
+
items: array(n.items, 1e4).map((v2) => {
|
|
541
|
+
const i = record(v2), a = i.activity === null ? null : record(i.activity), m2 = message(i.message, "item");
|
|
542
|
+
const documentKey = text(i.documentKey), branchId = i.branchId === null ? null : text(i.branchId);
|
|
543
|
+
if (m2.params.nodeId !== nodeId || m2.params.docKey !== documentKey || m2.params.branchId !== branchId)
|
|
544
|
+
return fail();
|
|
545
|
+
return {
|
|
546
|
+
documentKey,
|
|
547
|
+
branchId,
|
|
548
|
+
status: state(i.status),
|
|
549
|
+
message: m2,
|
|
550
|
+
startedAt: i.startedAt === null ? null : instant(i.startedAt),
|
|
551
|
+
endedAt: i.endedAt === null ? null : instant(i.endedAt),
|
|
552
|
+
activity: a === null ? null : {
|
|
553
|
+
kind: choice(a.kind, ["llm_call", "subagent", "ocr_call"]),
|
|
554
|
+
attempt: a.attempt === null ? null : integer(a.attempt, 1),
|
|
555
|
+
batch: a.batch === null ? null : integer(a.batch, 1),
|
|
556
|
+
batches: a.batches === null ? null : integer(a.batches, 1)
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
})
|
|
560
|
+
};
|
|
561
|
+
}),
|
|
562
|
+
...raw.publishedAt === void 0 ? {} : { publishedAt: instant(raw.publishedAt) }
|
|
563
|
+
};
|
|
564
|
+
const phaseKey = result.phase === "awaiting_review" ? "awaitingReview" : result.phase;
|
|
565
|
+
if (result.message.key !== `resume.${phaseKey}` || result.eventId !== `${result.executionResultId}:${result.revision}:${result.stateVersion}`)
|
|
566
|
+
return fail();
|
|
567
|
+
return result;
|
|
568
|
+
}
|
|
569
|
+
function parseResumeProgressResponse(value) {
|
|
570
|
+
const event = parseExecutionProgress(value), labels = record(record(value).labels);
|
|
571
|
+
const label = (v) => {
|
|
572
|
+
if (v === null) return null;
|
|
573
|
+
if (typeof v !== "string") return fail();
|
|
574
|
+
return [...v.replace(/\s+/g, " ")].filter((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127).join("").trim().slice(0, 512) || null;
|
|
575
|
+
};
|
|
576
|
+
return {
|
|
577
|
+
...event,
|
|
578
|
+
labels: {
|
|
579
|
+
nodes: Object.fromEntries(
|
|
580
|
+
Object.entries(record(labels.nodes)).map(([k, v]) => {
|
|
581
|
+
const n = record(v);
|
|
582
|
+
return [text(k), { name: label(n.name), type: text(n.type) }];
|
|
583
|
+
})
|
|
584
|
+
),
|
|
585
|
+
documents: Object.fromEntries(
|
|
586
|
+
Object.entries(record(labels.documents)).map(([k, v]) => [text(k), label(v)])
|
|
587
|
+
),
|
|
588
|
+
branches: Object.fromEntries(
|
|
589
|
+
Object.entries(record(labels.branches)).map(([k, v]) => [text(k), label(v)])
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
232
595
|
// src/sdk/reviews.ts
|
|
233
596
|
function segment(value) {
|
|
234
597
|
if (!value || value === "." || value === ".." || /[\\/]/.test(value) || [...value].some((char) => char.charCodeAt(0) < 32))
|
|
@@ -240,6 +603,38 @@ var Reviews = class {
|
|
|
240
603
|
this.http = http;
|
|
241
604
|
}
|
|
242
605
|
http;
|
|
606
|
+
/** One non-consuming snapshot read for opening/reconnecting a view. No polling. */
|
|
607
|
+
async progress(executionId, options) {
|
|
608
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
609
|
+
const response = await this.http.requestRaw(
|
|
610
|
+
"GET",
|
|
611
|
+
`/executions/${segment(executionId)}/review/progress`,
|
|
612
|
+
{
|
|
613
|
+
signal: options.signal,
|
|
614
|
+
query: { organization: options.organization }
|
|
615
|
+
}
|
|
616
|
+
);
|
|
617
|
+
let body;
|
|
618
|
+
try {
|
|
619
|
+
body = await response.text();
|
|
620
|
+
} catch (error) {
|
|
621
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
622
|
+
throw new DocmanaAbortError("Request aborted");
|
|
623
|
+
throw error;
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
const result = parseResumeProgressResponse(JSON.parse(body));
|
|
627
|
+
if (result.executionResultId !== executionId || result.organization !== options.organization)
|
|
628
|
+
throw new Error("Invalid progress scope");
|
|
629
|
+
return result;
|
|
630
|
+
} catch {
|
|
631
|
+
throw new DocmanaError("Invalid execution progress response", {
|
|
632
|
+
status: response.status,
|
|
633
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
634
|
+
code: "invalid_progress_response"
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
243
638
|
/** Read a page of HITL execution metadata without consuming any result. */
|
|
244
639
|
listExecutions(options) {
|
|
245
640
|
return this.http.requestJson("GET", "/reviews/executions", {
|
|
@@ -331,96 +726,6 @@ var Reviews = class {
|
|
|
331
726
|
|
|
332
727
|
// src/sdk/execution-events.ts
|
|
333
728
|
var import_node_crypto2 = require("crypto");
|
|
334
|
-
|
|
335
|
-
// src/sdk/review-events.ts
|
|
336
|
-
var import_node_crypto = require("crypto");
|
|
337
|
-
var REVIEW_EVENT_TYPES = [
|
|
338
|
-
"execution.awaiting_review",
|
|
339
|
-
"execution.review_done",
|
|
340
|
-
"execution.review_closed",
|
|
341
|
-
"execution.review_expired"
|
|
342
|
-
];
|
|
343
|
-
var ReviewEventVerificationError = class extends Error {
|
|
344
|
-
constructor(reason) {
|
|
345
|
-
super(`Invalid review event ${reason}`);
|
|
346
|
-
this.reason = reason;
|
|
347
|
-
this.name = "ReviewEventVerificationError";
|
|
348
|
-
}
|
|
349
|
-
reason;
|
|
350
|
-
};
|
|
351
|
-
function parseReviewEventCallback(value) {
|
|
352
|
-
const fail = () => {
|
|
353
|
-
throw new ReviewEventVerificationError("payload");
|
|
354
|
-
};
|
|
355
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
356
|
-
const raw = value;
|
|
357
|
-
const text = (key) => {
|
|
358
|
-
const v = raw[key];
|
|
359
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
360
|
-
return fail();
|
|
361
|
-
return v;
|
|
362
|
-
};
|
|
363
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
364
|
-
return fail();
|
|
365
|
-
const event = {
|
|
366
|
-
schemaVersion: 1,
|
|
367
|
-
eventId: text("eventId"),
|
|
368
|
-
type: raw.type,
|
|
369
|
-
organization: text("organization"),
|
|
370
|
-
executionResultId: text("executionResultId"),
|
|
371
|
-
reviewId: text("reviewId"),
|
|
372
|
-
publishedAt: text("publishedAt")
|
|
373
|
-
};
|
|
374
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
375
|
-
for (const key of ["flowId", "nodeId"])
|
|
376
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
377
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
378
|
-
if (raw[key] === void 0) continue;
|
|
379
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
380
|
-
event[key] = Number(raw[key]);
|
|
381
|
-
}
|
|
382
|
-
if (raw.documentKeys !== void 0) {
|
|
383
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
384
|
-
return fail();
|
|
385
|
-
event.documentKeys = raw.documentKeys;
|
|
386
|
-
}
|
|
387
|
-
if (raw.decision !== void 0) {
|
|
388
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
389
|
-
event.decision = raw.decision;
|
|
390
|
-
}
|
|
391
|
-
if (raw.reviewStatus !== void 0) {
|
|
392
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
393
|
-
event.reviewStatus = raw.reviewStatus;
|
|
394
|
-
}
|
|
395
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
396
|
-
return fail();
|
|
397
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
398
|
-
return event;
|
|
399
|
-
}
|
|
400
|
-
function verifyReviewEventCallback(options) {
|
|
401
|
-
const { body, signature, timestamp, secret } = options;
|
|
402
|
-
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
403
|
-
throw new ReviewEventVerificationError("timestamp");
|
|
404
|
-
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
405
|
-
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
406
|
-
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
407
|
-
throw new ReviewEventVerificationError("signature");
|
|
408
|
-
const expected = (0, import_node_crypto.createHmac)("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
409
|
-
if (!(0, import_node_crypto.timingSafeEqual)(expected, Buffer.from(signature.slice(3), "hex")))
|
|
410
|
-
throw new ReviewEventVerificationError("signature");
|
|
411
|
-
let value;
|
|
412
|
-
try {
|
|
413
|
-
value = JSON.parse(body);
|
|
414
|
-
} catch {
|
|
415
|
-
throw new ReviewEventVerificationError("payload");
|
|
416
|
-
}
|
|
417
|
-
const event = parseReviewEventCallback(value);
|
|
418
|
-
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
419
|
-
throw new ReviewEventVerificationError("organization");
|
|
420
|
-
return event;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// src/sdk/execution-events.ts
|
|
424
729
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
425
730
|
"execution.node.started",
|
|
426
731
|
"execution.node.completed",
|
|
@@ -429,42 +734,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
429
734
|
];
|
|
430
735
|
function parseExecutionEventCallback(value) {
|
|
431
736
|
const raw = value;
|
|
737
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
432
738
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
433
739
|
return parseReviewEventCallback(value);
|
|
434
|
-
const
|
|
740
|
+
const fail2 = () => {
|
|
435
741
|
throw new ReviewEventVerificationError("payload");
|
|
436
742
|
};
|
|
437
|
-
const
|
|
743
|
+
const text2 = (key) => {
|
|
438
744
|
const v = raw[key];
|
|
439
|
-
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v :
|
|
745
|
+
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v : fail2();
|
|
440
746
|
};
|
|
441
|
-
const
|
|
442
|
-
if (raw.schemaVersion !== 1) return
|
|
747
|
+
const integer2 = (key) => Number.isSafeInteger(raw[key]) && Number(raw[key]) >= 1 ? Number(raw[key]) : fail2();
|
|
748
|
+
if (raw.schemaVersion !== 1) return fail2();
|
|
443
749
|
const event = {
|
|
444
750
|
schemaVersion: 1,
|
|
445
|
-
eventId:
|
|
751
|
+
eventId: text2("eventId"),
|
|
446
752
|
type: raw.type,
|
|
447
|
-
organization:
|
|
448
|
-
executionResultId:
|
|
449
|
-
sequence:
|
|
450
|
-
occurredAt:
|
|
451
|
-
nodeId:
|
|
452
|
-
nodeType:
|
|
453
|
-
revision:
|
|
454
|
-
attemptId:
|
|
753
|
+
organization: text2("organization"),
|
|
754
|
+
executionResultId: text2("executionResultId"),
|
|
755
|
+
sequence: integer2("sequence"),
|
|
756
|
+
occurredAt: text2("occurredAt"),
|
|
757
|
+
nodeId: text2("nodeId"),
|
|
758
|
+
nodeType: text2("nodeType"),
|
|
759
|
+
revision: integer2("revision"),
|
|
760
|
+
attemptId: text2("attemptId"),
|
|
455
761
|
documentKeys: []
|
|
456
762
|
};
|
|
457
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
763
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
458
764
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
459
765
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
460
766
|
))
|
|
461
|
-
return
|
|
767
|
+
return fail2();
|
|
462
768
|
event.documentKeys = raw.documentKeys;
|
|
463
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
464
|
-
if (raw.flowVersion !== void 0) event.flowVersion =
|
|
769
|
+
if (raw.flowId !== void 0) event.flowId = text2("flowId");
|
|
770
|
+
if (raw.flowVersion !== void 0) event.flowVersion = integer2("flowVersion");
|
|
465
771
|
if (raw.publishedAt !== void 0) {
|
|
466
|
-
event.publishedAt =
|
|
467
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
772
|
+
event.publishedAt = text2("publishedAt");
|
|
773
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
468
774
|
}
|
|
469
775
|
return event;
|
|
470
776
|
}
|
|
@@ -519,114 +825,6 @@ var ExecutionEvents = class {
|
|
|
519
825
|
}
|
|
520
826
|
};
|
|
521
827
|
|
|
522
|
-
// src/sdk/errors.ts
|
|
523
|
-
var DocmanaError = class extends Error {
|
|
524
|
-
status;
|
|
525
|
-
requestId;
|
|
526
|
-
code;
|
|
527
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
528
|
-
apiCode;
|
|
529
|
-
reviewConflict;
|
|
530
|
-
constructor(message, opts = {}) {
|
|
531
|
-
super(message);
|
|
532
|
-
this.name = new.target.name;
|
|
533
|
-
this.status = opts.status;
|
|
534
|
-
this.requestId = opts.requestId;
|
|
535
|
-
this.code = opts.code ?? "docmana_error";
|
|
536
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
540
|
-
constructor(message, opts = {}) {
|
|
541
|
-
super(message, { ...opts, code: "auth_error" });
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
545
|
-
constructor(message, opts = {}) {
|
|
546
|
-
super(message, { ...opts, code: "permission_error" });
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
550
|
-
constructor(message, opts = {}) {
|
|
551
|
-
super(message, { ...opts, code: "not_found" });
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
555
|
-
constructor(message, opts = {}) {
|
|
556
|
-
super(message, { ...opts, code: "bad_request" });
|
|
557
|
-
}
|
|
558
|
-
};
|
|
559
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
560
|
-
constructor(message, opts = {}) {
|
|
561
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
562
|
-
}
|
|
563
|
-
};
|
|
564
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
565
|
-
errors;
|
|
566
|
-
result;
|
|
567
|
-
constructor(message, errors = [], result) {
|
|
568
|
-
super(message, { code: "execution_failed" });
|
|
569
|
-
this.errors = errors;
|
|
570
|
-
this.result = result;
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
574
|
-
jobId;
|
|
575
|
-
error;
|
|
576
|
-
result;
|
|
577
|
-
constructor(message, result) {
|
|
578
|
-
super(message, { code: "fraud_detection_failed" });
|
|
579
|
-
this.jobId = result.jobId;
|
|
580
|
-
this.error = result.error;
|
|
581
|
-
this.result = result;
|
|
582
|
-
}
|
|
583
|
-
};
|
|
584
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
585
|
-
constructor(message) {
|
|
586
|
-
super(message, { code: "timeout" });
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
590
|
-
constructor(message) {
|
|
591
|
-
super(message, { code: "aborted" });
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
595
|
-
retryAfterMs;
|
|
596
|
-
constructor(message, opts = {}) {
|
|
597
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
598
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
602
|
-
retryAfterMs;
|
|
603
|
-
constructor(message, opts = {}) {
|
|
604
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
605
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
606
|
-
}
|
|
607
|
-
};
|
|
608
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
609
|
-
const opts = { status, requestId };
|
|
610
|
-
switch (status) {
|
|
611
|
-
case 400:
|
|
612
|
-
return new DocmanaRequestError(message, opts);
|
|
613
|
-
case 401:
|
|
614
|
-
return new DocmanaAuthError(message, opts);
|
|
615
|
-
case 403:
|
|
616
|
-
return new DocmanaPermissionError(message, opts);
|
|
617
|
-
case 404:
|
|
618
|
-
return new DocmanaNotFoundError(message, opts);
|
|
619
|
-
case 413:
|
|
620
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
621
|
-
case 429:
|
|
622
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
623
|
-
case 503:
|
|
624
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
625
|
-
default:
|
|
626
|
-
return new DocmanaError(message, opts);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
828
|
// src/sdk/http/http-client.ts
|
|
631
829
|
var HttpClient = class {
|
|
632
830
|
constructor(opts) {
|
|
@@ -635,10 +833,10 @@ var HttpClient = class {
|
|
|
635
833
|
opts;
|
|
636
834
|
async requestJson(method, path, options = {}) {
|
|
637
835
|
const res = await this.requestRaw(method, path, options);
|
|
638
|
-
const
|
|
639
|
-
if (!
|
|
836
|
+
const text2 = await res.text();
|
|
837
|
+
if (!text2) return {};
|
|
640
838
|
try {
|
|
641
|
-
return JSON.parse(
|
|
839
|
+
return JSON.parse(text2);
|
|
642
840
|
} catch {
|
|
643
841
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
644
842
|
}
|
|
@@ -654,10 +852,10 @@ var HttpClient = class {
|
|
|
654
852
|
}
|
|
655
853
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
656
854
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
657
|
-
const
|
|
855
|
+
const message2 = await this.extractMessage(res);
|
|
658
856
|
const error = errorFromResponse(
|
|
659
857
|
res.status,
|
|
660
|
-
|
|
858
|
+
message2,
|
|
661
859
|
requestId,
|
|
662
860
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
663
861
|
);
|
|
@@ -716,15 +914,15 @@ function isRecord(value) {
|
|
|
716
914
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
717
915
|
}
|
|
718
916
|
function reviewConflictDetails(raw) {
|
|
719
|
-
const
|
|
917
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
720
918
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
721
919
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
722
|
-
const id =
|
|
723
|
-
const name =
|
|
920
|
+
const id = text2(actor.id, 200);
|
|
921
|
+
const name = text2(actor.name, 200);
|
|
724
922
|
const profile = {
|
|
725
|
-
firstName:
|
|
726
|
-
lastName:
|
|
727
|
-
email:
|
|
923
|
+
firstName: text2(actor.firstName, 200),
|
|
924
|
+
lastName: text2(actor.lastName, 200),
|
|
925
|
+
email: text2(actor.email, 200)
|
|
728
926
|
};
|
|
729
927
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
730
928
|
const decidedBy = [
|
|
@@ -744,13 +942,13 @@ function reviewConflictDetails(raw) {
|
|
|
744
942
|
}
|
|
745
943
|
}
|
|
746
944
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
747
|
-
const decidedAt =
|
|
945
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
748
946
|
return {
|
|
749
947
|
currentVersion,
|
|
750
948
|
decidedBy,
|
|
751
949
|
decidedAt,
|
|
752
950
|
version: currentVersion,
|
|
753
|
-
submittedBy: id ?? name ??
|
|
951
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
754
952
|
submittedAt: decidedAt
|
|
755
953
|
};
|
|
756
954
|
}
|
|
@@ -952,8 +1150,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
952
1150
|
query: organization ? { organization } : void 0,
|
|
953
1151
|
signal
|
|
954
1152
|
});
|
|
955
|
-
const
|
|
956
|
-
const body =
|
|
1153
|
+
const text2 = await res.text();
|
|
1154
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
957
1155
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
958
1156
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
959
1157
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -1021,8 +1219,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
1021
1219
|
signal
|
|
1022
1220
|
}
|
|
1023
1221
|
);
|
|
1024
|
-
const
|
|
1025
|
-
const body =
|
|
1222
|
+
const text2 = await res.text();
|
|
1223
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
1026
1224
|
const normalized = {
|
|
1027
1225
|
...body,
|
|
1028
1226
|
status: normalizeStatus(body.status)
|
|
@@ -1418,12 +1616,16 @@ function isTransientPollingError(error) {
|
|
|
1418
1616
|
DocmanaServiceUnavailableError,
|
|
1419
1617
|
DocmanaTimeoutError,
|
|
1420
1618
|
EXECUTION_NODE_EVENT_TYPES,
|
|
1619
|
+
EXECUTION_PROGRESS_TYPE,
|
|
1421
1620
|
ExecutionEvents,
|
|
1422
1621
|
FormulaAuthoring,
|
|
1622
|
+
PROGRESS_MESSAGE_KEYS,
|
|
1423
1623
|
REVIEW_EVENT_TYPES,
|
|
1424
1624
|
ReviewEventVerificationError,
|
|
1425
1625
|
Reviews,
|
|
1426
1626
|
parseExecutionEventCallback,
|
|
1627
|
+
parseExecutionProgress,
|
|
1628
|
+
parseResumeProgressResponse,
|
|
1427
1629
|
parseReviewEventCallback,
|
|
1428
1630
|
verifyExecutionEventCallback,
|
|
1429
1631
|
verifyReviewEventCallback
|