@docmana/sdk 0.45.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 +13 -0
- package/README.md +36 -0
- package/dist/cli.mjs +428 -219
- 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 +452 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +448 -235
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +43 -2
- package/package.json +13 -9
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,53 @@ 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
|
+
}
|
|
638
|
+
/** Read a page of HITL execution metadata without consuming any result. */
|
|
639
|
+
listExecutions(options) {
|
|
640
|
+
return this.http.requestJson("GET", "/reviews/executions", {
|
|
641
|
+
signal: options.signal,
|
|
642
|
+
query: {
|
|
643
|
+
organization: options.organization,
|
|
644
|
+
...options.flowId !== void 0 ? { flowId: options.flowId } : {},
|
|
645
|
+
...options.executionId !== void 0 ? { executionId: options.executionId } : {},
|
|
646
|
+
...options.limit !== void 0 ? { limit: String(options.limit) } : {},
|
|
647
|
+
...options.offset !== void 0 ? { offset: String(options.offset) } : {},
|
|
648
|
+
...options.sortBy !== void 0 ? { sortBy: options.sortBy } : {},
|
|
649
|
+
...options.sortDirection !== void 0 ? { sortDirection: options.sortDirection } : {}
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
}
|
|
243
653
|
/** Repeatable metadata read: never consumes the final execution result. */
|
|
244
654
|
calculation(executionId, options) {
|
|
245
655
|
return this.http.requestJson("GET", `/executions/${segment(executionId)}/review/calculation`, {
|
|
@@ -316,96 +726,6 @@ var Reviews = class {
|
|
|
316
726
|
|
|
317
727
|
// src/sdk/execution-events.ts
|
|
318
728
|
var import_node_crypto2 = require("crypto");
|
|
319
|
-
|
|
320
|
-
// src/sdk/review-events.ts
|
|
321
|
-
var import_node_crypto = require("crypto");
|
|
322
|
-
var REVIEW_EVENT_TYPES = [
|
|
323
|
-
"execution.awaiting_review",
|
|
324
|
-
"execution.review_done",
|
|
325
|
-
"execution.review_closed",
|
|
326
|
-
"execution.review_expired"
|
|
327
|
-
];
|
|
328
|
-
var ReviewEventVerificationError = class extends Error {
|
|
329
|
-
constructor(reason) {
|
|
330
|
-
super(`Invalid review event ${reason}`);
|
|
331
|
-
this.reason = reason;
|
|
332
|
-
this.name = "ReviewEventVerificationError";
|
|
333
|
-
}
|
|
334
|
-
reason;
|
|
335
|
-
};
|
|
336
|
-
function parseReviewEventCallback(value) {
|
|
337
|
-
const fail = () => {
|
|
338
|
-
throw new ReviewEventVerificationError("payload");
|
|
339
|
-
};
|
|
340
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
341
|
-
const raw = value;
|
|
342
|
-
const text = (key) => {
|
|
343
|
-
const v = raw[key];
|
|
344
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
345
|
-
return fail();
|
|
346
|
-
return v;
|
|
347
|
-
};
|
|
348
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
349
|
-
return fail();
|
|
350
|
-
const event = {
|
|
351
|
-
schemaVersion: 1,
|
|
352
|
-
eventId: text("eventId"),
|
|
353
|
-
type: raw.type,
|
|
354
|
-
organization: text("organization"),
|
|
355
|
-
executionResultId: text("executionResultId"),
|
|
356
|
-
reviewId: text("reviewId"),
|
|
357
|
-
publishedAt: text("publishedAt")
|
|
358
|
-
};
|
|
359
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
360
|
-
for (const key of ["flowId", "nodeId"])
|
|
361
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
362
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
363
|
-
if (raw[key] === void 0) continue;
|
|
364
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
365
|
-
event[key] = Number(raw[key]);
|
|
366
|
-
}
|
|
367
|
-
if (raw.documentKeys !== void 0) {
|
|
368
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
369
|
-
return fail();
|
|
370
|
-
event.documentKeys = raw.documentKeys;
|
|
371
|
-
}
|
|
372
|
-
if (raw.decision !== void 0) {
|
|
373
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
374
|
-
event.decision = raw.decision;
|
|
375
|
-
}
|
|
376
|
-
if (raw.reviewStatus !== void 0) {
|
|
377
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
378
|
-
event.reviewStatus = raw.reviewStatus;
|
|
379
|
-
}
|
|
380
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
381
|
-
return fail();
|
|
382
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
383
|
-
return event;
|
|
384
|
-
}
|
|
385
|
-
function verifyReviewEventCallback(options) {
|
|
386
|
-
const { body, signature, timestamp, secret } = options;
|
|
387
|
-
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
388
|
-
throw new ReviewEventVerificationError("timestamp");
|
|
389
|
-
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
390
|
-
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
391
|
-
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
392
|
-
throw new ReviewEventVerificationError("signature");
|
|
393
|
-
const expected = (0, import_node_crypto.createHmac)("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
394
|
-
if (!(0, import_node_crypto.timingSafeEqual)(expected, Buffer.from(signature.slice(3), "hex")))
|
|
395
|
-
throw new ReviewEventVerificationError("signature");
|
|
396
|
-
let value;
|
|
397
|
-
try {
|
|
398
|
-
value = JSON.parse(body);
|
|
399
|
-
} catch {
|
|
400
|
-
throw new ReviewEventVerificationError("payload");
|
|
401
|
-
}
|
|
402
|
-
const event = parseReviewEventCallback(value);
|
|
403
|
-
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
404
|
-
throw new ReviewEventVerificationError("organization");
|
|
405
|
-
return event;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
// src/sdk/execution-events.ts
|
|
409
729
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
410
730
|
"execution.node.started",
|
|
411
731
|
"execution.node.completed",
|
|
@@ -414,42 +734,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
414
734
|
];
|
|
415
735
|
function parseExecutionEventCallback(value) {
|
|
416
736
|
const raw = value;
|
|
737
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
417
738
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
418
739
|
return parseReviewEventCallback(value);
|
|
419
|
-
const
|
|
740
|
+
const fail2 = () => {
|
|
420
741
|
throw new ReviewEventVerificationError("payload");
|
|
421
742
|
};
|
|
422
|
-
const
|
|
743
|
+
const text2 = (key) => {
|
|
423
744
|
const v = raw[key];
|
|
424
|
-
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();
|
|
425
746
|
};
|
|
426
|
-
const
|
|
427
|
-
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();
|
|
428
749
|
const event = {
|
|
429
750
|
schemaVersion: 1,
|
|
430
|
-
eventId:
|
|
751
|
+
eventId: text2("eventId"),
|
|
431
752
|
type: raw.type,
|
|
432
|
-
organization:
|
|
433
|
-
executionResultId:
|
|
434
|
-
sequence:
|
|
435
|
-
occurredAt:
|
|
436
|
-
nodeId:
|
|
437
|
-
nodeType:
|
|
438
|
-
revision:
|
|
439
|
-
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"),
|
|
440
761
|
documentKeys: []
|
|
441
762
|
};
|
|
442
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
763
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
443
764
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
444
765
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
445
766
|
))
|
|
446
|
-
return
|
|
767
|
+
return fail2();
|
|
447
768
|
event.documentKeys = raw.documentKeys;
|
|
448
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
449
|
-
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");
|
|
450
771
|
if (raw.publishedAt !== void 0) {
|
|
451
|
-
event.publishedAt =
|
|
452
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
772
|
+
event.publishedAt = text2("publishedAt");
|
|
773
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
453
774
|
}
|
|
454
775
|
return event;
|
|
455
776
|
}
|
|
@@ -504,114 +825,6 @@ var ExecutionEvents = class {
|
|
|
504
825
|
}
|
|
505
826
|
};
|
|
506
827
|
|
|
507
|
-
// src/sdk/errors.ts
|
|
508
|
-
var DocmanaError = class extends Error {
|
|
509
|
-
status;
|
|
510
|
-
requestId;
|
|
511
|
-
code;
|
|
512
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
513
|
-
apiCode;
|
|
514
|
-
reviewConflict;
|
|
515
|
-
constructor(message, opts = {}) {
|
|
516
|
-
super(message);
|
|
517
|
-
this.name = new.target.name;
|
|
518
|
-
this.status = opts.status;
|
|
519
|
-
this.requestId = opts.requestId;
|
|
520
|
-
this.code = opts.code ?? "docmana_error";
|
|
521
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
522
|
-
}
|
|
523
|
-
};
|
|
524
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
525
|
-
constructor(message, opts = {}) {
|
|
526
|
-
super(message, { ...opts, code: "auth_error" });
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
530
|
-
constructor(message, opts = {}) {
|
|
531
|
-
super(message, { ...opts, code: "permission_error" });
|
|
532
|
-
}
|
|
533
|
-
};
|
|
534
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
535
|
-
constructor(message, opts = {}) {
|
|
536
|
-
super(message, { ...opts, code: "not_found" });
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
540
|
-
constructor(message, opts = {}) {
|
|
541
|
-
super(message, { ...opts, code: "bad_request" });
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
545
|
-
constructor(message, opts = {}) {
|
|
546
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
550
|
-
errors;
|
|
551
|
-
result;
|
|
552
|
-
constructor(message, errors = [], result) {
|
|
553
|
-
super(message, { code: "execution_failed" });
|
|
554
|
-
this.errors = errors;
|
|
555
|
-
this.result = result;
|
|
556
|
-
}
|
|
557
|
-
};
|
|
558
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
559
|
-
jobId;
|
|
560
|
-
error;
|
|
561
|
-
result;
|
|
562
|
-
constructor(message, result) {
|
|
563
|
-
super(message, { code: "fraud_detection_failed" });
|
|
564
|
-
this.jobId = result.jobId;
|
|
565
|
-
this.error = result.error;
|
|
566
|
-
this.result = result;
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
570
|
-
constructor(message) {
|
|
571
|
-
super(message, { code: "timeout" });
|
|
572
|
-
}
|
|
573
|
-
};
|
|
574
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
575
|
-
constructor(message) {
|
|
576
|
-
super(message, { code: "aborted" });
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
580
|
-
retryAfterMs;
|
|
581
|
-
constructor(message, opts = {}) {
|
|
582
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
583
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
584
|
-
}
|
|
585
|
-
};
|
|
586
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
587
|
-
retryAfterMs;
|
|
588
|
-
constructor(message, opts = {}) {
|
|
589
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
590
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
591
|
-
}
|
|
592
|
-
};
|
|
593
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
594
|
-
const opts = { status, requestId };
|
|
595
|
-
switch (status) {
|
|
596
|
-
case 400:
|
|
597
|
-
return new DocmanaRequestError(message, opts);
|
|
598
|
-
case 401:
|
|
599
|
-
return new DocmanaAuthError(message, opts);
|
|
600
|
-
case 403:
|
|
601
|
-
return new DocmanaPermissionError(message, opts);
|
|
602
|
-
case 404:
|
|
603
|
-
return new DocmanaNotFoundError(message, opts);
|
|
604
|
-
case 413:
|
|
605
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
606
|
-
case 429:
|
|
607
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
608
|
-
case 503:
|
|
609
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
610
|
-
default:
|
|
611
|
-
return new DocmanaError(message, opts);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
|
|
615
828
|
// src/sdk/http/http-client.ts
|
|
616
829
|
var HttpClient = class {
|
|
617
830
|
constructor(opts) {
|
|
@@ -620,10 +833,10 @@ var HttpClient = class {
|
|
|
620
833
|
opts;
|
|
621
834
|
async requestJson(method, path, options = {}) {
|
|
622
835
|
const res = await this.requestRaw(method, path, options);
|
|
623
|
-
const
|
|
624
|
-
if (!
|
|
836
|
+
const text2 = await res.text();
|
|
837
|
+
if (!text2) return {};
|
|
625
838
|
try {
|
|
626
|
-
return JSON.parse(
|
|
839
|
+
return JSON.parse(text2);
|
|
627
840
|
} catch {
|
|
628
841
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
629
842
|
}
|
|
@@ -639,10 +852,10 @@ var HttpClient = class {
|
|
|
639
852
|
}
|
|
640
853
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
641
854
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
642
|
-
const
|
|
855
|
+
const message2 = await this.extractMessage(res);
|
|
643
856
|
const error = errorFromResponse(
|
|
644
857
|
res.status,
|
|
645
|
-
|
|
858
|
+
message2,
|
|
646
859
|
requestId,
|
|
647
860
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
648
861
|
);
|
|
@@ -701,15 +914,15 @@ function isRecord(value) {
|
|
|
701
914
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
702
915
|
}
|
|
703
916
|
function reviewConflictDetails(raw) {
|
|
704
|
-
const
|
|
917
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
705
918
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
706
919
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
707
|
-
const id =
|
|
708
|
-
const name =
|
|
920
|
+
const id = text2(actor.id, 200);
|
|
921
|
+
const name = text2(actor.name, 200);
|
|
709
922
|
const profile = {
|
|
710
|
-
firstName:
|
|
711
|
-
lastName:
|
|
712
|
-
email:
|
|
923
|
+
firstName: text2(actor.firstName, 200),
|
|
924
|
+
lastName: text2(actor.lastName, 200),
|
|
925
|
+
email: text2(actor.email, 200)
|
|
713
926
|
};
|
|
714
927
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
715
928
|
const decidedBy = [
|
|
@@ -729,13 +942,13 @@ function reviewConflictDetails(raw) {
|
|
|
729
942
|
}
|
|
730
943
|
}
|
|
731
944
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
732
|
-
const decidedAt =
|
|
945
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
733
946
|
return {
|
|
734
947
|
currentVersion,
|
|
735
948
|
decidedBy,
|
|
736
949
|
decidedAt,
|
|
737
950
|
version: currentVersion,
|
|
738
|
-
submittedBy: id ?? name ??
|
|
951
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
739
952
|
submittedAt: decidedAt
|
|
740
953
|
};
|
|
741
954
|
}
|
|
@@ -937,8 +1150,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
937
1150
|
query: organization ? { organization } : void 0,
|
|
938
1151
|
signal
|
|
939
1152
|
});
|
|
940
|
-
const
|
|
941
|
-
const body =
|
|
1153
|
+
const text2 = await res.text();
|
|
1154
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
942
1155
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
943
1156
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
944
1157
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -1006,8 +1219,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
1006
1219
|
signal
|
|
1007
1220
|
}
|
|
1008
1221
|
);
|
|
1009
|
-
const
|
|
1010
|
-
const body =
|
|
1222
|
+
const text2 = await res.text();
|
|
1223
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
1011
1224
|
const normalized = {
|
|
1012
1225
|
...body,
|
|
1013
1226
|
status: normalizeStatus(body.status)
|
|
@@ -1403,12 +1616,16 @@ function isTransientPollingError(error) {
|
|
|
1403
1616
|
DocmanaServiceUnavailableError,
|
|
1404
1617
|
DocmanaTimeoutError,
|
|
1405
1618
|
EXECUTION_NODE_EVENT_TYPES,
|
|
1619
|
+
EXECUTION_PROGRESS_TYPE,
|
|
1406
1620
|
ExecutionEvents,
|
|
1407
1621
|
FormulaAuthoring,
|
|
1622
|
+
PROGRESS_MESSAGE_KEYS,
|
|
1408
1623
|
REVIEW_EVENT_TYPES,
|
|
1409
1624
|
ReviewEventVerificationError,
|
|
1410
1625
|
Reviews,
|
|
1411
1626
|
parseExecutionEventCallback,
|
|
1627
|
+
parseExecutionProgress,
|
|
1628
|
+
parseResumeProgressResponse,
|
|
1412
1629
|
parseReviewEventCallback,
|
|
1413
1630
|
verifyExecutionEventCallback,
|
|
1414
1631
|
verifyReviewEventCallback
|