@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.mjs
CHANGED
|
@@ -182,6 +182,365 @@ var FormulaAuthoring = class {
|
|
|
182
182
|
}
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
+
// src/sdk/errors.ts
|
|
186
|
+
var DocmanaError = class extends Error {
|
|
187
|
+
status;
|
|
188
|
+
requestId;
|
|
189
|
+
code;
|
|
190
|
+
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
191
|
+
apiCode;
|
|
192
|
+
reviewConflict;
|
|
193
|
+
constructor(message2, opts = {}) {
|
|
194
|
+
super(message2);
|
|
195
|
+
this.name = new.target.name;
|
|
196
|
+
this.status = opts.status;
|
|
197
|
+
this.requestId = opts.requestId;
|
|
198
|
+
this.code = opts.code ?? "docmana_error";
|
|
199
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var DocmanaAuthError = class extends DocmanaError {
|
|
203
|
+
constructor(message2, opts = {}) {
|
|
204
|
+
super(message2, { ...opts, code: "auth_error" });
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
var DocmanaPermissionError = class extends DocmanaError {
|
|
208
|
+
constructor(message2, opts = {}) {
|
|
209
|
+
super(message2, { ...opts, code: "permission_error" });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var DocmanaNotFoundError = class extends DocmanaError {
|
|
213
|
+
constructor(message2, opts = {}) {
|
|
214
|
+
super(message2, { ...opts, code: "not_found" });
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var DocmanaRequestError = class extends DocmanaError {
|
|
218
|
+
constructor(message2, opts = {}) {
|
|
219
|
+
super(message2, { ...opts, code: "bad_request" });
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
223
|
+
constructor(message2, opts = {}) {
|
|
224
|
+
super(message2, { ...opts, code: "payload_too_large" });
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var DocmanaExecutionError = class extends DocmanaError {
|
|
228
|
+
errors;
|
|
229
|
+
result;
|
|
230
|
+
constructor(message2, errors = [], result) {
|
|
231
|
+
super(message2, { code: "execution_failed" });
|
|
232
|
+
this.errors = errors;
|
|
233
|
+
this.result = result;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
237
|
+
jobId;
|
|
238
|
+
error;
|
|
239
|
+
result;
|
|
240
|
+
constructor(message2, result) {
|
|
241
|
+
super(message2, { code: "fraud_detection_failed" });
|
|
242
|
+
this.jobId = result.jobId;
|
|
243
|
+
this.error = result.error;
|
|
244
|
+
this.result = result;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
var DocmanaTimeoutError = class extends DocmanaError {
|
|
248
|
+
constructor(message2) {
|
|
249
|
+
super(message2, { code: "timeout" });
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var DocmanaAbortError = class extends DocmanaError {
|
|
253
|
+
constructor(message2) {
|
|
254
|
+
super(message2, { code: "aborted" });
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var DocmanaRateLimitError = class extends DocmanaError {
|
|
258
|
+
retryAfterMs;
|
|
259
|
+
constructor(message2, opts = {}) {
|
|
260
|
+
super(message2, { ...opts, code: "rate_limited" });
|
|
261
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
265
|
+
retryAfterMs;
|
|
266
|
+
constructor(message2, opts = {}) {
|
|
267
|
+
super(message2, { ...opts, code: "service_unavailable" });
|
|
268
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
function errorFromResponse(status, message2, requestId, retryAfterMs) {
|
|
272
|
+
const opts = { status, requestId };
|
|
273
|
+
switch (status) {
|
|
274
|
+
case 400:
|
|
275
|
+
return new DocmanaRequestError(message2, opts);
|
|
276
|
+
case 401:
|
|
277
|
+
return new DocmanaAuthError(message2, opts);
|
|
278
|
+
case 403:
|
|
279
|
+
return new DocmanaPermissionError(message2, opts);
|
|
280
|
+
case 404:
|
|
281
|
+
return new DocmanaNotFoundError(message2, opts);
|
|
282
|
+
case 413:
|
|
283
|
+
return new DocmanaPayloadTooLargeError(message2, opts);
|
|
284
|
+
case 429:
|
|
285
|
+
return new DocmanaRateLimitError(message2, { ...opts, retryAfterMs });
|
|
286
|
+
case 503:
|
|
287
|
+
return new DocmanaServiceUnavailableError(message2, { ...opts, retryAfterMs });
|
|
288
|
+
default:
|
|
289
|
+
return new DocmanaError(message2, opts);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/sdk/review-events.ts
|
|
294
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
295
|
+
var REVIEW_EVENT_TYPES = [
|
|
296
|
+
"execution.awaiting_review",
|
|
297
|
+
"execution.review_done",
|
|
298
|
+
"execution.review_closed",
|
|
299
|
+
"execution.review_expired"
|
|
300
|
+
];
|
|
301
|
+
var ReviewEventVerificationError = class extends Error {
|
|
302
|
+
constructor(reason) {
|
|
303
|
+
super(`Invalid review event ${reason}`);
|
|
304
|
+
this.reason = reason;
|
|
305
|
+
this.name = "ReviewEventVerificationError";
|
|
306
|
+
}
|
|
307
|
+
reason;
|
|
308
|
+
};
|
|
309
|
+
function parseReviewEventCallback(value) {
|
|
310
|
+
const fail2 = () => {
|
|
311
|
+
throw new ReviewEventVerificationError("payload");
|
|
312
|
+
};
|
|
313
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return fail2();
|
|
314
|
+
const raw = value;
|
|
315
|
+
const text2 = (key) => {
|
|
316
|
+
const v = raw[key];
|
|
317
|
+
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
318
|
+
return fail2();
|
|
319
|
+
return v;
|
|
320
|
+
};
|
|
321
|
+
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
322
|
+
return fail2();
|
|
323
|
+
const event = {
|
|
324
|
+
schemaVersion: 1,
|
|
325
|
+
eventId: text2("eventId"),
|
|
326
|
+
type: raw.type,
|
|
327
|
+
organization: text2("organization"),
|
|
328
|
+
executionResultId: text2("executionResultId"),
|
|
329
|
+
reviewId: text2("reviewId"),
|
|
330
|
+
publishedAt: text2("publishedAt")
|
|
331
|
+
};
|
|
332
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
333
|
+
for (const key of ["flowId", "nodeId"])
|
|
334
|
+
if (raw[key] !== void 0) event[key] = text2(key);
|
|
335
|
+
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
336
|
+
if (raw[key] === void 0) continue;
|
|
337
|
+
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail2();
|
|
338
|
+
event[key] = Number(raw[key]);
|
|
339
|
+
}
|
|
340
|
+
if (raw.documentKeys !== void 0) {
|
|
341
|
+
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
342
|
+
return fail2();
|
|
343
|
+
event.documentKeys = raw.documentKeys;
|
|
344
|
+
}
|
|
345
|
+
if (raw.decision !== void 0) {
|
|
346
|
+
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail2();
|
|
347
|
+
event.decision = raw.decision;
|
|
348
|
+
}
|
|
349
|
+
if (raw.reviewStatus !== void 0) {
|
|
350
|
+
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail2();
|
|
351
|
+
event.reviewStatus = raw.reviewStatus;
|
|
352
|
+
}
|
|
353
|
+
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
354
|
+
return fail2();
|
|
355
|
+
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail2();
|
|
356
|
+
return event;
|
|
357
|
+
}
|
|
358
|
+
function verifyReviewEventCallback(options) {
|
|
359
|
+
const { body, signature, timestamp, secret } = options;
|
|
360
|
+
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
361
|
+
throw new ReviewEventVerificationError("timestamp");
|
|
362
|
+
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
363
|
+
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
364
|
+
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
365
|
+
throw new ReviewEventVerificationError("signature");
|
|
366
|
+
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
367
|
+
if (!timingSafeEqual(expected, Buffer.from(signature.slice(3), "hex")))
|
|
368
|
+
throw new ReviewEventVerificationError("signature");
|
|
369
|
+
let value;
|
|
370
|
+
try {
|
|
371
|
+
value = JSON.parse(body);
|
|
372
|
+
} catch {
|
|
373
|
+
throw new ReviewEventVerificationError("payload");
|
|
374
|
+
}
|
|
375
|
+
const event = parseReviewEventCallback(value);
|
|
376
|
+
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
377
|
+
throw new ReviewEventVerificationError("organization");
|
|
378
|
+
return event;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/sdk/resume-progress.ts
|
|
382
|
+
var EXECUTION_PROGRESS_TYPE = "execution.progress";
|
|
383
|
+
var PROGRESS_MESSAGE_KEYS = [
|
|
384
|
+
"resume.running",
|
|
385
|
+
"resume.awaitingReview",
|
|
386
|
+
"resume.completed",
|
|
387
|
+
"resume.rejected",
|
|
388
|
+
"resume.failed",
|
|
389
|
+
"node.pending",
|
|
390
|
+
"node.running",
|
|
391
|
+
"node.done",
|
|
392
|
+
"node.failed",
|
|
393
|
+
"node.reused",
|
|
394
|
+
"node.skipped",
|
|
395
|
+
"item.pending",
|
|
396
|
+
"item.running",
|
|
397
|
+
"item.llmCall",
|
|
398
|
+
"item.llmCallRetry",
|
|
399
|
+
"item.subagent",
|
|
400
|
+
"item.subagentBatch",
|
|
401
|
+
"item.ocrCall",
|
|
402
|
+
"item.done",
|
|
403
|
+
"item.failed",
|
|
404
|
+
"item.reused",
|
|
405
|
+
"item.skippedAncestorFailed",
|
|
406
|
+
"item.skippedBranchNotTaken"
|
|
407
|
+
];
|
|
408
|
+
var fail = () => {
|
|
409
|
+
throw new ReviewEventVerificationError("payload");
|
|
410
|
+
};
|
|
411
|
+
function record(value) {
|
|
412
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : fail();
|
|
413
|
+
}
|
|
414
|
+
function text(value) {
|
|
415
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 && [...value].every((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127) ? value : fail();
|
|
416
|
+
}
|
|
417
|
+
function instant(value) {
|
|
418
|
+
const v = text(value);
|
|
419
|
+
return /^\d{4}-\d{2}-\d{2}T.+(?:Z|[+-]\d{2}:\d{2})$/.test(v) && Number.isFinite(Date.parse(v)) ? v : fail();
|
|
420
|
+
}
|
|
421
|
+
var integer = (v, min = 0) => Number.isSafeInteger(v) && Number(v) >= min ? Number(v) : fail();
|
|
422
|
+
var bool = (v) => typeof v === "boolean" ? v : fail();
|
|
423
|
+
function choice(v, values) {
|
|
424
|
+
return values.includes(v) ? v : fail();
|
|
425
|
+
}
|
|
426
|
+
function array(v, max) {
|
|
427
|
+
return Array.isArray(v) && v.length <= max ? v : fail();
|
|
428
|
+
}
|
|
429
|
+
var state = (v) => choice(v, ["pending", "running", "done", "failed", "skipped", "reused"]);
|
|
430
|
+
function message(value, prefix) {
|
|
431
|
+
const raw = record(value), p = record(raw.params), params = {};
|
|
432
|
+
const key = choice(raw.key, PROGRESS_MESSAGE_KEYS);
|
|
433
|
+
if (!key.startsWith(prefix + ".")) return fail();
|
|
434
|
+
if (prefix !== "resume") params.nodeId = text(p.nodeId);
|
|
435
|
+
if (prefix === "node") {
|
|
436
|
+
params.done = integer(p.done);
|
|
437
|
+
params.total = integer(p.total);
|
|
438
|
+
}
|
|
439
|
+
if (prefix === "item") {
|
|
440
|
+
params.docKey = text(p.docKey);
|
|
441
|
+
params.branchId = p.branchId === null ? null : text(p.branchId);
|
|
442
|
+
for (const k of ["attempt", "batch", "batches"])
|
|
443
|
+
if (p[k] !== void 0) params[k] = integer(p[k], 1);
|
|
444
|
+
}
|
|
445
|
+
return { key, params };
|
|
446
|
+
}
|
|
447
|
+
function parseExecutionProgress(value) {
|
|
448
|
+
const raw = record(value);
|
|
449
|
+
if (raw.schemaVersion !== 1 || raw.type !== EXECUTION_PROGRESS_TYPE) return fail();
|
|
450
|
+
const result = {
|
|
451
|
+
schemaVersion: 1,
|
|
452
|
+
type: EXECUTION_PROGRESS_TYPE,
|
|
453
|
+
eventId: text(raw.eventId),
|
|
454
|
+
organization: text(raw.organization),
|
|
455
|
+
executionResultId: text(raw.executionResultId),
|
|
456
|
+
flowId: text(raw.flowId),
|
|
457
|
+
flowVersion: raw.flowVersion === null ? null : integer(raw.flowVersion, 1),
|
|
458
|
+
reviewId: text(raw.reviewId),
|
|
459
|
+
revision: instant(raw.revision),
|
|
460
|
+
stateVersion: integer(raw.stateVersion, 1),
|
|
461
|
+
phase: choice(raw.phase, [
|
|
462
|
+
"running",
|
|
463
|
+
"awaiting_review",
|
|
464
|
+
"completed",
|
|
465
|
+
"rejected",
|
|
466
|
+
"failed"
|
|
467
|
+
]),
|
|
468
|
+
message: message(raw.message, "resume"),
|
|
469
|
+
updatedAt: instant(raw.updatedAt),
|
|
470
|
+
itemsTruncated: bool(raw.itemsTruncated),
|
|
471
|
+
nodesTruncated: bool(raw.nodesTruncated),
|
|
472
|
+
nodes: array(raw.nodes, 5e3).map((v) => {
|
|
473
|
+
const n = record(v), nodeId = text(n.nodeId), documents = record(n.documents), m = message(n.message, "node");
|
|
474
|
+
if (m.params.nodeId !== nodeId) return fail();
|
|
475
|
+
return {
|
|
476
|
+
nodeId,
|
|
477
|
+
nodeType: text(n.nodeType),
|
|
478
|
+
status: state(n.status),
|
|
479
|
+
message: m,
|
|
480
|
+
documents: {
|
|
481
|
+
total: integer(documents.total),
|
|
482
|
+
pending: integer(documents.pending),
|
|
483
|
+
running: integer(documents.running),
|
|
484
|
+
done: integer(documents.done),
|
|
485
|
+
failed: integer(documents.failed),
|
|
486
|
+
skipped: integer(documents.skipped),
|
|
487
|
+
reused: integer(documents.reused)
|
|
488
|
+
},
|
|
489
|
+
items: array(n.items, 1e4).map((v2) => {
|
|
490
|
+
const i = record(v2), a = i.activity === null ? null : record(i.activity), m2 = message(i.message, "item");
|
|
491
|
+
const documentKey = text(i.documentKey), branchId = i.branchId === null ? null : text(i.branchId);
|
|
492
|
+
if (m2.params.nodeId !== nodeId || m2.params.docKey !== documentKey || m2.params.branchId !== branchId)
|
|
493
|
+
return fail();
|
|
494
|
+
return {
|
|
495
|
+
documentKey,
|
|
496
|
+
branchId,
|
|
497
|
+
status: state(i.status),
|
|
498
|
+
message: m2,
|
|
499
|
+
startedAt: i.startedAt === null ? null : instant(i.startedAt),
|
|
500
|
+
endedAt: i.endedAt === null ? null : instant(i.endedAt),
|
|
501
|
+
activity: a === null ? null : {
|
|
502
|
+
kind: choice(a.kind, ["llm_call", "subagent", "ocr_call"]),
|
|
503
|
+
attempt: a.attempt === null ? null : integer(a.attempt, 1),
|
|
504
|
+
batch: a.batch === null ? null : integer(a.batch, 1),
|
|
505
|
+
batches: a.batches === null ? null : integer(a.batches, 1)
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
})
|
|
509
|
+
};
|
|
510
|
+
}),
|
|
511
|
+
...raw.publishedAt === void 0 ? {} : { publishedAt: instant(raw.publishedAt) }
|
|
512
|
+
};
|
|
513
|
+
const phaseKey = result.phase === "awaiting_review" ? "awaitingReview" : result.phase;
|
|
514
|
+
if (result.message.key !== `resume.${phaseKey}` || result.eventId !== `${result.executionResultId}:${result.revision}:${result.stateVersion}`)
|
|
515
|
+
return fail();
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
function parseResumeProgressResponse(value) {
|
|
519
|
+
const event = parseExecutionProgress(value), labels = record(record(value).labels);
|
|
520
|
+
const label = (v) => {
|
|
521
|
+
if (v === null) return null;
|
|
522
|
+
if (typeof v !== "string") return fail();
|
|
523
|
+
return [...v.replace(/\s+/g, " ")].filter((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127).join("").trim().slice(0, 512) || null;
|
|
524
|
+
};
|
|
525
|
+
return {
|
|
526
|
+
...event,
|
|
527
|
+
labels: {
|
|
528
|
+
nodes: Object.fromEntries(
|
|
529
|
+
Object.entries(record(labels.nodes)).map(([k, v]) => {
|
|
530
|
+
const n = record(v);
|
|
531
|
+
return [text(k), { name: label(n.name), type: text(n.type) }];
|
|
532
|
+
})
|
|
533
|
+
),
|
|
534
|
+
documents: Object.fromEntries(
|
|
535
|
+
Object.entries(record(labels.documents)).map(([k, v]) => [text(k), label(v)])
|
|
536
|
+
),
|
|
537
|
+
branches: Object.fromEntries(
|
|
538
|
+
Object.entries(record(labels.branches)).map(([k, v]) => [text(k), label(v)])
|
|
539
|
+
)
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
185
544
|
// src/sdk/reviews.ts
|
|
186
545
|
function segment(value) {
|
|
187
546
|
if (!value || value === "." || value === ".." || /[\\/]/.test(value) || [...value].some((char) => char.charCodeAt(0) < 32))
|
|
@@ -193,6 +552,38 @@ var Reviews = class {
|
|
|
193
552
|
this.http = http;
|
|
194
553
|
}
|
|
195
554
|
http;
|
|
555
|
+
/** One non-consuming snapshot read for opening/reconnecting a view. No polling. */
|
|
556
|
+
async progress(executionId, options) {
|
|
557
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
558
|
+
const response = await this.http.requestRaw(
|
|
559
|
+
"GET",
|
|
560
|
+
`/executions/${segment(executionId)}/review/progress`,
|
|
561
|
+
{
|
|
562
|
+
signal: options.signal,
|
|
563
|
+
query: { organization: options.organization }
|
|
564
|
+
}
|
|
565
|
+
);
|
|
566
|
+
let body;
|
|
567
|
+
try {
|
|
568
|
+
body = await response.text();
|
|
569
|
+
} catch (error) {
|
|
570
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
571
|
+
throw new DocmanaAbortError("Request aborted");
|
|
572
|
+
throw error;
|
|
573
|
+
}
|
|
574
|
+
try {
|
|
575
|
+
const result = parseResumeProgressResponse(JSON.parse(body));
|
|
576
|
+
if (result.executionResultId !== executionId || result.organization !== options.organization)
|
|
577
|
+
throw new Error("Invalid progress scope");
|
|
578
|
+
return result;
|
|
579
|
+
} catch {
|
|
580
|
+
throw new DocmanaError("Invalid execution progress response", {
|
|
581
|
+
status: response.status,
|
|
582
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
583
|
+
code: "invalid_progress_response"
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
}
|
|
196
587
|
/** Read a page of HITL execution metadata without consuming any result. */
|
|
197
588
|
listExecutions(options) {
|
|
198
589
|
return this.http.requestJson("GET", "/reviews/executions", {
|
|
@@ -284,96 +675,6 @@ var Reviews = class {
|
|
|
284
675
|
|
|
285
676
|
// src/sdk/execution-events.ts
|
|
286
677
|
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
287
|
-
|
|
288
|
-
// src/sdk/review-events.ts
|
|
289
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
290
|
-
var REVIEW_EVENT_TYPES = [
|
|
291
|
-
"execution.awaiting_review",
|
|
292
|
-
"execution.review_done",
|
|
293
|
-
"execution.review_closed",
|
|
294
|
-
"execution.review_expired"
|
|
295
|
-
];
|
|
296
|
-
var ReviewEventVerificationError = class extends Error {
|
|
297
|
-
constructor(reason) {
|
|
298
|
-
super(`Invalid review event ${reason}`);
|
|
299
|
-
this.reason = reason;
|
|
300
|
-
this.name = "ReviewEventVerificationError";
|
|
301
|
-
}
|
|
302
|
-
reason;
|
|
303
|
-
};
|
|
304
|
-
function parseReviewEventCallback(value) {
|
|
305
|
-
const fail = () => {
|
|
306
|
-
throw new ReviewEventVerificationError("payload");
|
|
307
|
-
};
|
|
308
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
309
|
-
const raw = value;
|
|
310
|
-
const text = (key) => {
|
|
311
|
-
const v = raw[key];
|
|
312
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
313
|
-
return fail();
|
|
314
|
-
return v;
|
|
315
|
-
};
|
|
316
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
317
|
-
return fail();
|
|
318
|
-
const event = {
|
|
319
|
-
schemaVersion: 1,
|
|
320
|
-
eventId: text("eventId"),
|
|
321
|
-
type: raw.type,
|
|
322
|
-
organization: text("organization"),
|
|
323
|
-
executionResultId: text("executionResultId"),
|
|
324
|
-
reviewId: text("reviewId"),
|
|
325
|
-
publishedAt: text("publishedAt")
|
|
326
|
-
};
|
|
327
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
328
|
-
for (const key of ["flowId", "nodeId"])
|
|
329
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
330
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
331
|
-
if (raw[key] === void 0) continue;
|
|
332
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
333
|
-
event[key] = Number(raw[key]);
|
|
334
|
-
}
|
|
335
|
-
if (raw.documentKeys !== void 0) {
|
|
336
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
337
|
-
return fail();
|
|
338
|
-
event.documentKeys = raw.documentKeys;
|
|
339
|
-
}
|
|
340
|
-
if (raw.decision !== void 0) {
|
|
341
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
342
|
-
event.decision = raw.decision;
|
|
343
|
-
}
|
|
344
|
-
if (raw.reviewStatus !== void 0) {
|
|
345
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
346
|
-
event.reviewStatus = raw.reviewStatus;
|
|
347
|
-
}
|
|
348
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
349
|
-
return fail();
|
|
350
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
351
|
-
return event;
|
|
352
|
-
}
|
|
353
|
-
function verifyReviewEventCallback(options) {
|
|
354
|
-
const { body, signature, timestamp, secret } = options;
|
|
355
|
-
if (!timestamp || !/^\d{10,13}$/.test(timestamp))
|
|
356
|
-
throw new ReviewEventVerificationError("timestamp");
|
|
357
|
-
const age = Math.abs((options.nowMs ?? Date.now()) / 1e3 - Number(timestamp));
|
|
358
|
-
if (age > (options.toleranceSeconds ?? 300)) throw new ReviewEventVerificationError("timestamp");
|
|
359
|
-
if (secret.length < 32 || !signature || !/^v1=[a-f0-9]{64}$/.test(signature))
|
|
360
|
-
throw new ReviewEventVerificationError("signature");
|
|
361
|
-
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest();
|
|
362
|
-
if (!timingSafeEqual(expected, Buffer.from(signature.slice(3), "hex")))
|
|
363
|
-
throw new ReviewEventVerificationError("signature");
|
|
364
|
-
let value;
|
|
365
|
-
try {
|
|
366
|
-
value = JSON.parse(body);
|
|
367
|
-
} catch {
|
|
368
|
-
throw new ReviewEventVerificationError("payload");
|
|
369
|
-
}
|
|
370
|
-
const event = parseReviewEventCallback(value);
|
|
371
|
-
if (options.organization !== void 0 && event.organization !== options.organization)
|
|
372
|
-
throw new ReviewEventVerificationError("organization");
|
|
373
|
-
return event;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
// src/sdk/execution-events.ts
|
|
377
678
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
378
679
|
"execution.node.started",
|
|
379
680
|
"execution.node.completed",
|
|
@@ -382,42 +683,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
382
683
|
];
|
|
383
684
|
function parseExecutionEventCallback(value) {
|
|
384
685
|
const raw = value;
|
|
686
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
385
687
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
386
688
|
return parseReviewEventCallback(value);
|
|
387
|
-
const
|
|
689
|
+
const fail2 = () => {
|
|
388
690
|
throw new ReviewEventVerificationError("payload");
|
|
389
691
|
};
|
|
390
|
-
const
|
|
692
|
+
const text2 = (key) => {
|
|
391
693
|
const v = raw[key];
|
|
392
|
-
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();
|
|
393
695
|
};
|
|
394
|
-
const
|
|
395
|
-
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();
|
|
396
698
|
const event = {
|
|
397
699
|
schemaVersion: 1,
|
|
398
|
-
eventId:
|
|
700
|
+
eventId: text2("eventId"),
|
|
399
701
|
type: raw.type,
|
|
400
|
-
organization:
|
|
401
|
-
executionResultId:
|
|
402
|
-
sequence:
|
|
403
|
-
occurredAt:
|
|
404
|
-
nodeId:
|
|
405
|
-
nodeType:
|
|
406
|
-
revision:
|
|
407
|
-
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"),
|
|
408
710
|
documentKeys: []
|
|
409
711
|
};
|
|
410
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
712
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
411
713
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
412
714
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
413
715
|
))
|
|
414
|
-
return
|
|
716
|
+
return fail2();
|
|
415
717
|
event.documentKeys = raw.documentKeys;
|
|
416
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
417
|
-
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");
|
|
418
720
|
if (raw.publishedAt !== void 0) {
|
|
419
|
-
event.publishedAt =
|
|
420
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
721
|
+
event.publishedAt = text2("publishedAt");
|
|
722
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
421
723
|
}
|
|
422
724
|
return event;
|
|
423
725
|
}
|
|
@@ -472,114 +774,6 @@ var ExecutionEvents = class {
|
|
|
472
774
|
}
|
|
473
775
|
};
|
|
474
776
|
|
|
475
|
-
// src/sdk/errors.ts
|
|
476
|
-
var DocmanaError = class extends Error {
|
|
477
|
-
status;
|
|
478
|
-
requestId;
|
|
479
|
-
code;
|
|
480
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
481
|
-
apiCode;
|
|
482
|
-
reviewConflict;
|
|
483
|
-
constructor(message, opts = {}) {
|
|
484
|
-
super(message);
|
|
485
|
-
this.name = new.target.name;
|
|
486
|
-
this.status = opts.status;
|
|
487
|
-
this.requestId = opts.requestId;
|
|
488
|
-
this.code = opts.code ?? "docmana_error";
|
|
489
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
493
|
-
constructor(message, opts = {}) {
|
|
494
|
-
super(message, { ...opts, code: "auth_error" });
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
498
|
-
constructor(message, opts = {}) {
|
|
499
|
-
super(message, { ...opts, code: "permission_error" });
|
|
500
|
-
}
|
|
501
|
-
};
|
|
502
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
503
|
-
constructor(message, opts = {}) {
|
|
504
|
-
super(message, { ...opts, code: "not_found" });
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
508
|
-
constructor(message, opts = {}) {
|
|
509
|
-
super(message, { ...opts, code: "bad_request" });
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
513
|
-
constructor(message, opts = {}) {
|
|
514
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
518
|
-
errors;
|
|
519
|
-
result;
|
|
520
|
-
constructor(message, errors = [], result) {
|
|
521
|
-
super(message, { code: "execution_failed" });
|
|
522
|
-
this.errors = errors;
|
|
523
|
-
this.result = result;
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
527
|
-
jobId;
|
|
528
|
-
error;
|
|
529
|
-
result;
|
|
530
|
-
constructor(message, result) {
|
|
531
|
-
super(message, { code: "fraud_detection_failed" });
|
|
532
|
-
this.jobId = result.jobId;
|
|
533
|
-
this.error = result.error;
|
|
534
|
-
this.result = result;
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
538
|
-
constructor(message) {
|
|
539
|
-
super(message, { code: "timeout" });
|
|
540
|
-
}
|
|
541
|
-
};
|
|
542
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
543
|
-
constructor(message) {
|
|
544
|
-
super(message, { code: "aborted" });
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
548
|
-
retryAfterMs;
|
|
549
|
-
constructor(message, opts = {}) {
|
|
550
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
551
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
555
|
-
retryAfterMs;
|
|
556
|
-
constructor(message, opts = {}) {
|
|
557
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
558
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
562
|
-
const opts = { status, requestId };
|
|
563
|
-
switch (status) {
|
|
564
|
-
case 400:
|
|
565
|
-
return new DocmanaRequestError(message, opts);
|
|
566
|
-
case 401:
|
|
567
|
-
return new DocmanaAuthError(message, opts);
|
|
568
|
-
case 403:
|
|
569
|
-
return new DocmanaPermissionError(message, opts);
|
|
570
|
-
case 404:
|
|
571
|
-
return new DocmanaNotFoundError(message, opts);
|
|
572
|
-
case 413:
|
|
573
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
574
|
-
case 429:
|
|
575
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
576
|
-
case 503:
|
|
577
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
578
|
-
default:
|
|
579
|
-
return new DocmanaError(message, opts);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
777
|
// src/sdk/http/http-client.ts
|
|
584
778
|
var HttpClient = class {
|
|
585
779
|
constructor(opts) {
|
|
@@ -588,10 +782,10 @@ var HttpClient = class {
|
|
|
588
782
|
opts;
|
|
589
783
|
async requestJson(method, path, options = {}) {
|
|
590
784
|
const res = await this.requestRaw(method, path, options);
|
|
591
|
-
const
|
|
592
|
-
if (!
|
|
785
|
+
const text2 = await res.text();
|
|
786
|
+
if (!text2) return {};
|
|
593
787
|
try {
|
|
594
|
-
return JSON.parse(
|
|
788
|
+
return JSON.parse(text2);
|
|
595
789
|
} catch {
|
|
596
790
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
597
791
|
}
|
|
@@ -607,10 +801,10 @@ var HttpClient = class {
|
|
|
607
801
|
}
|
|
608
802
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
609
803
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
610
|
-
const
|
|
804
|
+
const message2 = await this.extractMessage(res);
|
|
611
805
|
const error = errorFromResponse(
|
|
612
806
|
res.status,
|
|
613
|
-
|
|
807
|
+
message2,
|
|
614
808
|
requestId,
|
|
615
809
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
616
810
|
);
|
|
@@ -669,15 +863,15 @@ function isRecord(value) {
|
|
|
669
863
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
670
864
|
}
|
|
671
865
|
function reviewConflictDetails(raw) {
|
|
672
|
-
const
|
|
866
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
673
867
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
674
868
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
675
|
-
const id =
|
|
676
|
-
const name =
|
|
869
|
+
const id = text2(actor.id, 200);
|
|
870
|
+
const name = text2(actor.name, 200);
|
|
677
871
|
const profile = {
|
|
678
|
-
firstName:
|
|
679
|
-
lastName:
|
|
680
|
-
email:
|
|
872
|
+
firstName: text2(actor.firstName, 200),
|
|
873
|
+
lastName: text2(actor.lastName, 200),
|
|
874
|
+
email: text2(actor.email, 200)
|
|
681
875
|
};
|
|
682
876
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
683
877
|
const decidedBy = [
|
|
@@ -697,13 +891,13 @@ function reviewConflictDetails(raw) {
|
|
|
697
891
|
}
|
|
698
892
|
}
|
|
699
893
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
700
|
-
const decidedAt =
|
|
894
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
701
895
|
return {
|
|
702
896
|
currentVersion,
|
|
703
897
|
decidedBy,
|
|
704
898
|
decidedAt,
|
|
705
899
|
version: currentVersion,
|
|
706
|
-
submittedBy: id ?? name ??
|
|
900
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
707
901
|
submittedAt: decidedAt
|
|
708
902
|
};
|
|
709
903
|
}
|
|
@@ -905,8 +1099,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
905
1099
|
query: organization ? { organization } : void 0,
|
|
906
1100
|
signal
|
|
907
1101
|
});
|
|
908
|
-
const
|
|
909
|
-
const body =
|
|
1102
|
+
const text2 = await res.text();
|
|
1103
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
910
1104
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
911
1105
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
912
1106
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -974,8 +1168,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
974
1168
|
signal
|
|
975
1169
|
}
|
|
976
1170
|
);
|
|
977
|
-
const
|
|
978
|
-
const body =
|
|
1171
|
+
const text2 = await res.text();
|
|
1172
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
979
1173
|
const normalized = {
|
|
980
1174
|
...body,
|
|
981
1175
|
status: normalizeStatus(body.status)
|
|
@@ -1370,12 +1564,16 @@ export {
|
|
|
1370
1564
|
DocmanaServiceUnavailableError,
|
|
1371
1565
|
DocmanaTimeoutError,
|
|
1372
1566
|
EXECUTION_NODE_EVENT_TYPES,
|
|
1567
|
+
EXECUTION_PROGRESS_TYPE,
|
|
1373
1568
|
ExecutionEvents,
|
|
1374
1569
|
FormulaAuthoring,
|
|
1570
|
+
PROGRESS_MESSAGE_KEYS,
|
|
1375
1571
|
REVIEW_EVENT_TYPES,
|
|
1376
1572
|
ReviewEventVerificationError,
|
|
1377
1573
|
Reviews,
|
|
1378
1574
|
parseExecutionEventCallback,
|
|
1575
|
+
parseExecutionProgress,
|
|
1576
|
+
parseResumeProgressResponse,
|
|
1379
1577
|
parseReviewEventCallback,
|
|
1380
1578
|
verifyExecutionEventCallback,
|
|
1381
1579
|
verifyReviewEventCallback
|