@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.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,53 @@ 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
|
+
}
|
|
587
|
+
/** Read a page of HITL execution metadata without consuming any result. */
|
|
588
|
+
listExecutions(options) {
|
|
589
|
+
return this.http.requestJson("GET", "/reviews/executions", {
|
|
590
|
+
signal: options.signal,
|
|
591
|
+
query: {
|
|
592
|
+
organization: options.organization,
|
|
593
|
+
...options.flowId !== void 0 ? { flowId: options.flowId } : {},
|
|
594
|
+
...options.executionId !== void 0 ? { executionId: options.executionId } : {},
|
|
595
|
+
...options.limit !== void 0 ? { limit: String(options.limit) } : {},
|
|
596
|
+
...options.offset !== void 0 ? { offset: String(options.offset) } : {},
|
|
597
|
+
...options.sortBy !== void 0 ? { sortBy: options.sortBy } : {},
|
|
598
|
+
...options.sortDirection !== void 0 ? { sortDirection: options.sortDirection } : {}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
}
|
|
196
602
|
/** Repeatable metadata read: never consumes the final execution result. */
|
|
197
603
|
calculation(executionId, options) {
|
|
198
604
|
return this.http.requestJson("GET", `/executions/${segment(executionId)}/review/calculation`, {
|
|
@@ -269,96 +675,6 @@ var Reviews = class {
|
|
|
269
675
|
|
|
270
676
|
// src/sdk/execution-events.ts
|
|
271
677
|
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
272
|
-
|
|
273
|
-
// src/sdk/review-events.ts
|
|
274
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
275
|
-
var REVIEW_EVENT_TYPES = [
|
|
276
|
-
"execution.awaiting_review",
|
|
277
|
-
"execution.review_done",
|
|
278
|
-
"execution.review_closed",
|
|
279
|
-
"execution.review_expired"
|
|
280
|
-
];
|
|
281
|
-
var ReviewEventVerificationError = class extends Error {
|
|
282
|
-
constructor(reason) {
|
|
283
|
-
super(`Invalid review event ${reason}`);
|
|
284
|
-
this.reason = reason;
|
|
285
|
-
this.name = "ReviewEventVerificationError";
|
|
286
|
-
}
|
|
287
|
-
reason;
|
|
288
|
-
};
|
|
289
|
-
function parseReviewEventCallback(value) {
|
|
290
|
-
const fail = () => {
|
|
291
|
-
throw new ReviewEventVerificationError("payload");
|
|
292
|
-
};
|
|
293
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
294
|
-
const raw = value;
|
|
295
|
-
const text = (key) => {
|
|
296
|
-
const v = raw[key];
|
|
297
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
298
|
-
return fail();
|
|
299
|
-
return v;
|
|
300
|
-
};
|
|
301
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
302
|
-
return fail();
|
|
303
|
-
const event = {
|
|
304
|
-
schemaVersion: 1,
|
|
305
|
-
eventId: text("eventId"),
|
|
306
|
-
type: raw.type,
|
|
307
|
-
organization: text("organization"),
|
|
308
|
-
executionResultId: text("executionResultId"),
|
|
309
|
-
reviewId: text("reviewId"),
|
|
310
|
-
publishedAt: text("publishedAt")
|
|
311
|
-
};
|
|
312
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
313
|
-
for (const key of ["flowId", "nodeId"])
|
|
314
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
315
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
316
|
-
if (raw[key] === void 0) continue;
|
|
317
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
318
|
-
event[key] = Number(raw[key]);
|
|
319
|
-
}
|
|
320
|
-
if (raw.documentKeys !== void 0) {
|
|
321
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
322
|
-
return fail();
|
|
323
|
-
event.documentKeys = raw.documentKeys;
|
|
324
|
-
}
|
|
325
|
-
if (raw.decision !== void 0) {
|
|
326
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
327
|
-
event.decision = raw.decision;
|
|
328
|
-
}
|
|
329
|
-
if (raw.reviewStatus !== void 0) {
|
|
330
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
331
|
-
event.reviewStatus = raw.reviewStatus;
|
|
332
|
-
}
|
|
333
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
334
|
-
return fail();
|
|
335
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
336
|
-
return event;
|
|
337
|
-
}
|
|
338
|
-
function verifyReviewEventCallback(options) {
|
|
339
|
-
const { body, signature, timestamp, secret } = options;
|
|
340
|
-
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
341
|
-
throw new ReviewEventVerificationError("timestamp");
|
|
342
|
-
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
343
|
-
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
344
|
-
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
345
|
-
throw new ReviewEventVerificationError("signature");
|
|
346
|
-
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
347
|
-
if (!timingSafeEqual(expected, Buffer.from(signature.slice(3), "hex")))
|
|
348
|
-
throw new ReviewEventVerificationError("signature");
|
|
349
|
-
let value;
|
|
350
|
-
try {
|
|
351
|
-
value = JSON.parse(body);
|
|
352
|
-
} catch {
|
|
353
|
-
throw new ReviewEventVerificationError("payload");
|
|
354
|
-
}
|
|
355
|
-
const event = parseReviewEventCallback(value);
|
|
356
|
-
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
357
|
-
throw new ReviewEventVerificationError("organization");
|
|
358
|
-
return event;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// src/sdk/execution-events.ts
|
|
362
678
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
363
679
|
"execution.node.started",
|
|
364
680
|
"execution.node.completed",
|
|
@@ -367,42 +683,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
367
683
|
];
|
|
368
684
|
function parseExecutionEventCallback(value) {
|
|
369
685
|
const raw = value;
|
|
686
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
370
687
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
371
688
|
return parseReviewEventCallback(value);
|
|
372
|
-
const
|
|
689
|
+
const fail2 = () => {
|
|
373
690
|
throw new ReviewEventVerificationError("payload");
|
|
374
691
|
};
|
|
375
|
-
const
|
|
692
|
+
const text2 = (key) => {
|
|
376
693
|
const v = raw[key];
|
|
377
|
-
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v :
|
|
694
|
+
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v : fail2();
|
|
378
695
|
};
|
|
379
|
-
const
|
|
380
|
-
if (raw.schemaVersion !== 1) return
|
|
696
|
+
const integer2 = (key) => Number.isSafeInteger(raw[key]) && Number(raw[key]) >= 1 ? Number(raw[key]) : fail2();
|
|
697
|
+
if (raw.schemaVersion !== 1) return fail2();
|
|
381
698
|
const event = {
|
|
382
699
|
schemaVersion: 1,
|
|
383
|
-
eventId:
|
|
700
|
+
eventId: text2("eventId"),
|
|
384
701
|
type: raw.type,
|
|
385
|
-
organization:
|
|
386
|
-
executionResultId:
|
|
387
|
-
sequence:
|
|
388
|
-
occurredAt:
|
|
389
|
-
nodeId:
|
|
390
|
-
nodeType:
|
|
391
|
-
revision:
|
|
392
|
-
attemptId:
|
|
702
|
+
organization: text2("organization"),
|
|
703
|
+
executionResultId: text2("executionResultId"),
|
|
704
|
+
sequence: integer2("sequence"),
|
|
705
|
+
occurredAt: text2("occurredAt"),
|
|
706
|
+
nodeId: text2("nodeId"),
|
|
707
|
+
nodeType: text2("nodeType"),
|
|
708
|
+
revision: integer2("revision"),
|
|
709
|
+
attemptId: text2("attemptId"),
|
|
393
710
|
documentKeys: []
|
|
394
711
|
};
|
|
395
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
712
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
396
713
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
397
714
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
398
715
|
))
|
|
399
|
-
return
|
|
716
|
+
return fail2();
|
|
400
717
|
event.documentKeys = raw.documentKeys;
|
|
401
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
402
|
-
if (raw.flowVersion !== void 0) event.flowVersion =
|
|
718
|
+
if (raw.flowId !== void 0) event.flowId = text2("flowId");
|
|
719
|
+
if (raw.flowVersion !== void 0) event.flowVersion = integer2("flowVersion");
|
|
403
720
|
if (raw.publishedAt !== void 0) {
|
|
404
|
-
event.publishedAt =
|
|
405
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
721
|
+
event.publishedAt = text2("publishedAt");
|
|
722
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
406
723
|
}
|
|
407
724
|
return event;
|
|
408
725
|
}
|
|
@@ -457,114 +774,6 @@ var ExecutionEvents = class {
|
|
|
457
774
|
}
|
|
458
775
|
};
|
|
459
776
|
|
|
460
|
-
// src/sdk/errors.ts
|
|
461
|
-
var DocmanaError = class extends Error {
|
|
462
|
-
status;
|
|
463
|
-
requestId;
|
|
464
|
-
code;
|
|
465
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
466
|
-
apiCode;
|
|
467
|
-
reviewConflict;
|
|
468
|
-
constructor(message, opts = {}) {
|
|
469
|
-
super(message);
|
|
470
|
-
this.name = new.target.name;
|
|
471
|
-
this.status = opts.status;
|
|
472
|
-
this.requestId = opts.requestId;
|
|
473
|
-
this.code = opts.code ?? "docmana_error";
|
|
474
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
478
|
-
constructor(message, opts = {}) {
|
|
479
|
-
super(message, { ...opts, code: "auth_error" });
|
|
480
|
-
}
|
|
481
|
-
};
|
|
482
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
483
|
-
constructor(message, opts = {}) {
|
|
484
|
-
super(message, { ...opts, code: "permission_error" });
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
488
|
-
constructor(message, opts = {}) {
|
|
489
|
-
super(message, { ...opts, code: "not_found" });
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
493
|
-
constructor(message, opts = {}) {
|
|
494
|
-
super(message, { ...opts, code: "bad_request" });
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
498
|
-
constructor(message, opts = {}) {
|
|
499
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
500
|
-
}
|
|
501
|
-
};
|
|
502
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
503
|
-
errors;
|
|
504
|
-
result;
|
|
505
|
-
constructor(message, errors = [], result) {
|
|
506
|
-
super(message, { code: "execution_failed" });
|
|
507
|
-
this.errors = errors;
|
|
508
|
-
this.result = result;
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
512
|
-
jobId;
|
|
513
|
-
error;
|
|
514
|
-
result;
|
|
515
|
-
constructor(message, result) {
|
|
516
|
-
super(message, { code: "fraud_detection_failed" });
|
|
517
|
-
this.jobId = result.jobId;
|
|
518
|
-
this.error = result.error;
|
|
519
|
-
this.result = result;
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
523
|
-
constructor(message) {
|
|
524
|
-
super(message, { code: "timeout" });
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
528
|
-
constructor(message) {
|
|
529
|
-
super(message, { code: "aborted" });
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
533
|
-
retryAfterMs;
|
|
534
|
-
constructor(message, opts = {}) {
|
|
535
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
536
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
540
|
-
retryAfterMs;
|
|
541
|
-
constructor(message, opts = {}) {
|
|
542
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
543
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
544
|
-
}
|
|
545
|
-
};
|
|
546
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
547
|
-
const opts = { status, requestId };
|
|
548
|
-
switch (status) {
|
|
549
|
-
case 400:
|
|
550
|
-
return new DocmanaRequestError(message, opts);
|
|
551
|
-
case 401:
|
|
552
|
-
return new DocmanaAuthError(message, opts);
|
|
553
|
-
case 403:
|
|
554
|
-
return new DocmanaPermissionError(message, opts);
|
|
555
|
-
case 404:
|
|
556
|
-
return new DocmanaNotFoundError(message, opts);
|
|
557
|
-
case 413:
|
|
558
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
559
|
-
case 429:
|
|
560
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
561
|
-
case 503:
|
|
562
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
563
|
-
default:
|
|
564
|
-
return new DocmanaError(message, opts);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
777
|
// src/sdk/http/http-client.ts
|
|
569
778
|
var HttpClient = class {
|
|
570
779
|
constructor(opts) {
|
|
@@ -573,10 +782,10 @@ var HttpClient = class {
|
|
|
573
782
|
opts;
|
|
574
783
|
async requestJson(method, path, options = {}) {
|
|
575
784
|
const res = await this.requestRaw(method, path, options);
|
|
576
|
-
const
|
|
577
|
-
if (!
|
|
785
|
+
const text2 = await res.text();
|
|
786
|
+
if (!text2) return {};
|
|
578
787
|
try {
|
|
579
|
-
return JSON.parse(
|
|
788
|
+
return JSON.parse(text2);
|
|
580
789
|
} catch {
|
|
581
790
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
582
791
|
}
|
|
@@ -592,10 +801,10 @@ var HttpClient = class {
|
|
|
592
801
|
}
|
|
593
802
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
594
803
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
595
|
-
const
|
|
804
|
+
const message2 = await this.extractMessage(res);
|
|
596
805
|
const error = errorFromResponse(
|
|
597
806
|
res.status,
|
|
598
|
-
|
|
807
|
+
message2,
|
|
599
808
|
requestId,
|
|
600
809
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
601
810
|
);
|
|
@@ -654,15 +863,15 @@ function isRecord(value) {
|
|
|
654
863
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
655
864
|
}
|
|
656
865
|
function reviewConflictDetails(raw) {
|
|
657
|
-
const
|
|
866
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
658
867
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
659
868
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
660
|
-
const id =
|
|
661
|
-
const name =
|
|
869
|
+
const id = text2(actor.id, 200);
|
|
870
|
+
const name = text2(actor.name, 200);
|
|
662
871
|
const profile = {
|
|
663
|
-
firstName:
|
|
664
|
-
lastName:
|
|
665
|
-
email:
|
|
872
|
+
firstName: text2(actor.firstName, 200),
|
|
873
|
+
lastName: text2(actor.lastName, 200),
|
|
874
|
+
email: text2(actor.email, 200)
|
|
666
875
|
};
|
|
667
876
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
668
877
|
const decidedBy = [
|
|
@@ -682,13 +891,13 @@ function reviewConflictDetails(raw) {
|
|
|
682
891
|
}
|
|
683
892
|
}
|
|
684
893
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
685
|
-
const decidedAt =
|
|
894
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
686
895
|
return {
|
|
687
896
|
currentVersion,
|
|
688
897
|
decidedBy,
|
|
689
898
|
decidedAt,
|
|
690
899
|
version: currentVersion,
|
|
691
|
-
submittedBy: id ?? name ??
|
|
900
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
692
901
|
submittedAt: decidedAt
|
|
693
902
|
};
|
|
694
903
|
}
|
|
@@ -890,8 +1099,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
890
1099
|
query: organization ? { organization } : void 0,
|
|
891
1100
|
signal
|
|
892
1101
|
});
|
|
893
|
-
const
|
|
894
|
-
const body =
|
|
1102
|
+
const text2 = await res.text();
|
|
1103
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
895
1104
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
896
1105
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
897
1106
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -959,8 +1168,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
959
1168
|
signal
|
|
960
1169
|
}
|
|
961
1170
|
);
|
|
962
|
-
const
|
|
963
|
-
const body =
|
|
1171
|
+
const text2 = await res.text();
|
|
1172
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
964
1173
|
const normalized = {
|
|
965
1174
|
...body,
|
|
966
1175
|
status: normalizeStatus(body.status)
|
|
@@ -1355,12 +1564,16 @@ export {
|
|
|
1355
1564
|
DocmanaServiceUnavailableError,
|
|
1356
1565
|
DocmanaTimeoutError,
|
|
1357
1566
|
EXECUTION_NODE_EVENT_TYPES,
|
|
1567
|
+
EXECUTION_PROGRESS_TYPE,
|
|
1358
1568
|
ExecutionEvents,
|
|
1359
1569
|
FormulaAuthoring,
|
|
1570
|
+
PROGRESS_MESSAGE_KEYS,
|
|
1360
1571
|
REVIEW_EVENT_TYPES,
|
|
1361
1572
|
ReviewEventVerificationError,
|
|
1362
1573
|
Reviews,
|
|
1363
1574
|
parseExecutionEventCallback,
|
|
1575
|
+
parseExecutionProgress,
|
|
1576
|
+
parseResumeProgressResponse,
|
|
1364
1577
|
parseReviewEventCallback,
|
|
1365
1578
|
verifyExecutionEventCallback,
|
|
1366
1579
|
verifyReviewEventCallback
|